vanderkorn / AspNet.Identity.MongoDB

An updated version of an Identity provider using MongoDB for storage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AspNet.Identity.MongoDB

An updated version of an ASP.NET Identity provider using MongoDB for storage. This started out as a fork on the original project MongoDB.AspNet.Identity by InspectorIT, but it seems the author has abandoned the project, so I've decided to create my own repository.

Build status

[Test status] (https://ci.appveyor.com/project/SteenTttrup/aspnet-identity-mongodb)

Purpose

ASP.NET MVC 5 shipped with a new Identity system (in the Microsoft.AspNet.Identity.Core package) in order to support both local login and remote logins via OpenID/OAuth, but only ships with an Entity Framework provider (Microsoft.AspNet.Identity.EntityFramework).

News

07-07-2016 - Updating to latest MongoDb driver (2.2.4) and fixing the async/await methods to work as intended.

22-11-2015 - The repository was created to take the code to the latest version of the Identity assemblies and the MongoDB driver.

Features

  • Drop-in replacement ASP.NET Identity with MongoDB as the backing store.
  • Requires only 2 mongo document type, one for users and one for roles
  • Supports additional profile properties on your application's user model.
  • Provides UserStore implementation that implements these interfaces:
    • IUserLoginStore
    • IUserClaimStore
    • IUserRoleStore
    • IUserPasswordStore
    • IUserSecurityStampStore
    • IQueryableUserStore
    • IUserEmailStore
    • IUserPhoneNumberStore
    • IUserTwoFactorStore<TUser, string>
    • IUserLockoutStore<TUser, string>
    • IUserStore
  • Provides RoleStore implementation that implements this interface:
    • IQueryableRoleStore

Instructions

These instructions assume you know how to set up MongoDB within an MVC application.

  1. Create a new ASP.NET MVC 5 project, choosing the Individual User Accounts authentication type.
  2. Remove the Entity Framework packages and replace with MongoDB Identity:
Uninstall-Package Microsoft.AspNet.Identity.EntityFramework
Uninstall-Package EntityFramework
Install-Package AspNetIdentity.MongoDB
  1. In ~/Models/IdentityModels.cs:
    • Remove the namespace: Microsoft.AspNet.Identity.EntityFramework
    • Add the namespace: AspNet.Identity.MongoDB
    • Remove the ApplicationDbContext class completely.
  2. In ~/Controllers/AccountController.cs
    • Remove the namespace: Microsoft.AspNet.Identity.EntityFramework
    • Add the connection string name to the constructor of the UserStore. Or empty constructor will use DefaultConnection
public AccountController()
{
    this.UserManager = new UserManager<ApplicationUser>(
        new UserStore<ApplicationUser>("Mongo");
}

If you expect your user/role stores to contain lots of users, you should make sure the proper indexes are in place on the MongoD collections. On application start you can call these 2 initialize methods, and the indexes will be added if they're missing.

public Application_Start()
{
        UserStore<ApplicationUser>.Initialize("Mongo");
        RoleStore<ApplicationUser>.Initialize("Mongo");
}

Or you can create the indexes yourself, directly in the MongoDb console. Here an example where the user collection is named 'user' and the role collection is named 'role'.

db.user.createIndex({ "lun": 1 }, { "unique": true })
db.user.createIndex({ "lem": 1 })
db.role.createIndex({ "ln": 1 }, { "unique": true })

Connection Strings

The UserStore has multiple constructors for handling connection strings. Here are some examples of the expected inputs and where the connection string should be located.

1. SQL Style

UserStore(string connectionNameOrUrl)

UserStore("Mongo")

web.config

<add name="Mongo" connectionString="Server=localhost:27017;Database={YourDataBase}" />

2. Mongo Style

UserStore(string connectionNameOrUrl)

UserStore("Mongo")

web.config

<add name="Mongo" connectionString="mongodb://localhost/{YourDataBase}" />

OR

UserStore(string connectionNameOrUrl)

UserStore("mongodb://localhost/{YourDataBase}")

About

An updated version of an Identity provider using MongoDB for storage.

License:MIT License


Languages

Language:C# 97.5%Language:PowerShell 2.5%