An updated version of an ASP.NET Identity provider using MongoDB for storage. This started out as a fork on the original project [https://github.com/InspectorIT/MongoDB.AspNet.Identity](MongoDB.AspNet.Identity by InspectorIT), but it seems the author has abandoned the project, so I've decided to create my own repository.
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).
22-11-2015 - The repository was created to take the code to the latest version of the Identity assemblies and the MongoDB driver.
- 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
These instructions assume you know how to set up MongoDB within an MVC application.
- Create a new ASP.NET MVC 5 project, choosing the Individual User Accounts authentication type.
- Remove the Entity Framework packages and replace with MongoDB Identity:
Uninstall-Package Microsoft.AspNet.Identity.EntityFramework
Uninstall-Package EntityFramework
Install-Package AspNetIdentity.MongoDB
- In ~/Models/IdentityModels.cs:
- Remove the namespace: Microsoft.AspNet.Identity.EntityFramework
- Add the namespace: AspNet.Identity.MongoDB
- Remove the ApplicationDbContext class completely.
- 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");
}
public Application_Start()
{
UserStore<ApplicationUser>.Initialize("Mongo");
RoleStore<ApplicationUser>.Initialize("Mongo");
}
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.
UserStore(string connectionNameOrUrl)
UserStore("Mongo")
web.config
<add name="Mongo" connectionString="Server=localhost:27017;Database={YourDataBase}" />
UserStore(string connectionNameOrUrl)
UserStore("Mongo")
web.config
<add name="Mongo" connectionString="mongodb://localhost/{YourDataBase}" />
OR
UserStore(string connectionNameOrUrl)
UserStore("mongodb://localhost/{YourDataBase}")