Skip to content

A MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 3.1

License

Notifications You must be signed in to change notification settings

spin973/AspNetCore.Identity.MongoDB

Repository files navigation

AspNetCore.Identity.MongoDB

This is the .NET 6.0 version of the repository AspNetCore.Identity.Mongo, it allows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 3.1.

Dot Net Core Versions support

Library supports ASP.NET Core 6.0.

Using the Library

The library is available at NuGet.org.

Sample

You can find a sample application under ./samples folder. This is a simple asp.net core 6.0 with scaffolding identity that use MongoDB as repository.

Tests

In order to be able to run the tests, you need to have MongoDB up and running on localhost:27017. You can easily do this by running the below Docker command:

docker run --name some-mongo -d -p "27017:27017" mongo:3

After that, you can run the tests through the dotnet test command under the test project directory.

How to use:

AspNetCore.Identity.MongoDB is installed from NuGet:

Install-Package AspNetCore.IdentityProvider.MongoDB

The simplest way to set up:

using AspNetCore.Identity.MongoDB;
using AspNetCore.Identity.MongoDB.Model;

// in Program.cs 
var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddIdentity<MongoDbUser>()
    .AddMongoDbStores(); //use defaults: MongoDbUser, MongoDbRole with primary key of type MongoDB.Bson.ObjectId

You can personalize the connections parameter with an Action<MongoDbOptions> parameter:

using AspNetCore.Identity.MongoDB;
using AspNetCore.Identity.MongoDB.Model;

// in Program.cs 
builder.Services
    .AddIdentity<MongoDbUser>()
    .AddMongoDbStores(options =>
    {
        options.ConnectionString = "mongodb://localhost:27017";  // default "mongodb://localhost/default"
        options.DatabaseName = "MongoIdentityDb";                // default "identityDB"
        options.UsersCollection = "myUsersCollection";           // default "identity.users"
        options.RolesCollection = "myRolesCollection";           // default "identity.roles"
    });

Or You can also configure an instance of MongoClientSettings:

using AspNetCore.Identity.MongoDB;
using AspNetCore.Identity.MongoDB.Model;
using MongoDB.Driver;

// in Program.cs 
builder.Services
    .AddIdentity<MongoDbUser, MongoDbRole>()
    .AddMongoDbStores(options =>
    {
        options.MongoClientSettings = new MongoClientSettings()
        {
            ApplicationName = "mongoApp",
            Credential = MongoCredential.CreateCredential("source", "username", "password"),
            Servers = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 27017) },
            // other options
        };
    });

Using User and Role models:

using AspNetCore.Identity.MongoDB;
using AspNetCore.Identity.MongoDB.Model;

// in Program.cs 
builder.Services
    .AddIdentity<MongoDbUser, MongoDbRole>()
    .AddMongoDbStores();

Using different type of the primary key (default is MongoDB.Bson.ObjectId):

using AspNetCore.Identity.MongoDB;
using AspNetCore.Identity.MongoDB.Model;

public class ApplicationUser : MongoDbUser<string>
{
}

public class ApplicationRole : MongoDbRole<string>
{
}

// in Program.cs 
builder.Services
    .AddIdentity<ApplicationUser, ApplicationRole>()
    .AddMongoDbStores();

How to Contribute

Before create any issue/PR please look at the CONTRIBUTING

Code of conduct

See CODE_OF_CONDUCT

License

This project is licensed under the MIT license

Support This Project

If you have found this project helpful, either as a library that you use or as a learning tool, please consider offering me a coffee: Buy Me A Coffee