Skip to content

Yitzchok/AspNet.Identity3.MongoDB

 
 

Repository files navigation

AspNet.Identity3.MongoDB

ASP.NET 5 uses new version of Identity system which has only Entity Framework provider. Goal of this project is to create provider which uses MongoDB as storage.

CoreCLR support

I use official MongoDB C# Driver which does not have CoreCLR support yet. As soon as they will start working with CoreCLR I'll update this project.

Installation

Add dependency to you project.json:

{
    ...
    "dependencies": {
	    ...,
	    "AspNet.Identity3.MongoDB": "0.0.1"
    },
    ...
}

Usage

Here I'm going to show how to use AspNet.Identity3.MongoDB in ASP.NET 5 MVC sample project.

User and Role Model

First we need models for users and roles. Create ApplicationUser derived from MongoIdentityUser:

public class ApplicationUser : MongoIdentityUser
{

}

For roles I'll use MongoIdentityRole class.

DbContext

Derive ApplicationDbContext class from MongoIdentityContext:

public class ApplicationDbContext : MongoIdentityContext<ApplicationUser, MongoIdentityRole>
{
    public ApplicationDbContext()
        : base()
    {
        string connectionString = "";
        string databaseName = "";
        var client = new MongoClient(connectionString);
        var database = client.GetDatabase(databaseName);

        this.Users = database.GetCollection<ApplicationUser>("users");
        this.Roles = database.GetCollection<MongoIdentityRole>("roles");
    }
}

Setting up

In Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ApplicationDbContext>();

    services.AddIdentity<ApplicationUser, MongoIdentityRole>(Configuration)
        .AddMongoStores<ApplicationDbContext, ApplicationUser, MongoIdentityRole>();

    ...
}

About

ASP.NET Identity 3 provider with MongoDB as storage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%