AspNetCore.Identity.Stores.Mongo 1.0.0

dotnet add package AspNetCore.Identity.Stores.Mongo --version 1.0.0                
NuGet\Install-Package AspNetCore.Identity.Stores.Mongo -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspNetCore.Identity.Stores.Mongo" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNetCore.Identity.Stores.Mongo --version 1.0.0                
#r "nuget: AspNetCore.Identity.Stores.Mongo, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AspNetCore.Identity.Stores.Mongo as a Cake Addin
#addin nuget:?package=AspNetCore.Identity.Stores.Mongo&version=1.0.0

// Install AspNetCore.Identity.Stores.Mongo as a Cake Tool
#tool nuget:?package=AspNetCore.Identity.Stores.Mongo&version=1.0.0                

AspNetCore.Identity.Stores.Mongo

License: MIT Code Coverage

This extension simplifies the integration of Identity stores in .NET 7 with MongoDB. </br>

It provides a ready-to-use implementation of the IUserStore and IRoleStore interfaces for working with users and roles stored in a MongoDB database.

Features

  • MongoDB-Based Storage: Replace the default SQL Server storage of ASP.NET Core Identity with MongoDB, a NoSQL database.

  • Customizable Options: Customize MongoDB settings such as GUID representation and collection names to suit your application's needs.

  • Optimized Queries: Utilize BsonDocument for optimized MongoDB queries to enhance performance.

Installation

You can install this library via NuGet Package Manager:

dotnet add package AspNetCore.Identity.Stores.Mongo

Getting Started

To start using this library, follow these steps:

1. Create Your Identity Models:

Create your own Identity models that inherit from MongoIdentityUser<TKey> and MongoIdentityRole<TKey> provided by this library. <br />

You can add custom properties and methods as needed.

public class ApplicationUser : MongoIdentityUser<Guid>
{
    public override Guid Id { get; set; } = Guid.NewGuid();
    
    // Your custom properties and methods
}

public class ApplicationRole : MongoIdentityRole<Guid>
{
    public override Guid Id { get; set; } = Guid.NewGuid();
    
    // Your custom properties and methods
}

*IMPORTANT 😗 if you don't want to override <b>Id</b> for Guid or ObjectId <b>Key</b>, you can use these class

for GUID : MongoIdentityUserAsGuid and MongoIdentityRoleAsGuid
for ObjectId : MongoIdentityUserAsObjectId and MongoIdentityRoleAsObjectId

2. Configure MongoDb Stores

builder.Services
    .AddIdentityCore<ApplicationUser>()
    .AddMongoDbStores<ApplicationUser, ApplicationRole>("YourMongoDBConnectionString", "YourDatabaseName");

// only user store ? 

builder.Services
    .AddIdentityCore<ApplicationUser>()
    .AddMongoDbStores<ApplicationUser>("YourMongoDBConnectionString", "YourDatabaseName");

Customizing MongoDB Options

You can customize MongoDB options by providing an action to the AddMongoDbStores method. </br>

For example, to change the <i>GUID representation</i> and <i>collection names</i> :

builder.Services
    .AddIdentityCore<ApplicationUser>()
    .AddMongoDbStores<ApplicationUser, ApplicationRole>(
        "YourMongoDBConnectionString",
        "YourDatabaseName",
        options =>
        {
            options.GuidRepresentation = GuidRepresentation.Standard;
            options.RoleCollectionName = "YourRoleCollectionName";
            options.UserCollectionName = "YourUserCollectionName";
        });

Optimized Queries with BSONDocument

In this library, i use BsonDocument to optimize queries when interacting with MongoDB.

For example, consider the FindUserLoginAsync method:

protected override async Task<TUserLogin?> FindUserLoginAsync(TKey userId, string loginProvider, string providerKey, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();
        
        ThrowIfDisposed();

        var matchUserIdDocument =new BsonDocument("$match", new BsonDocument("_id", ConvertIdToString(userId)));
        
        var unwindDocument = new BsonDocument("$unwind",
            new BsonDocument
            {
                { "path", "$Logins" },
                { "preserveNullAndEmptyArrays", false }
            });
        
        var matchDocument = new BsonDocument("$match",
            new BsonDocument("$and",
                new BsonArray
                {
                    new BsonDocument("Logins.LoginProvider", loginProvider),
                    new BsonDocument("Logins.ProviderKey", providerKey),
                }));

        var projectDocument = new BsonDocument("$project", new BsonDocument()
        {
            {"UserId", "$_id"},
            {"LoginProvider", 1},
            {"ProviderKey", 1},
            {"ProviderDisplayName", 1}
        });

        return (TUserLogin)(await UserCollection.AggregateAsync(
            new BsonDocumentStagePipelineDefinition<TUser, MongoIdentityUserLogin<TKey>>(new[]
                { matchUserIdDocument, unwindDocument, matchDocument, projectDocument }), cancellationToken: cancellationToken)
            .ConfigureAwait(false)).FirstOrDefault(cancellationToken);
    }

Acknowledgements

I would like to thank the ASP.NET Core Identity team for their excellent work on Identity and the MongoDB community for the MongoDB .NET driver.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 153 9/17/2023