Migrating Identity Management to MongoDB in .NET Core

Check here for updated version of this article.

So, you want to use .NET Core? Awesome!

An important part of .NET is its Identity Management which is sweet ... If you're not familiar with it, it simply means you don't need to write boilerplate code for User Management.

Traditionally, Identity Management uses EntityFramework and is written for SQL Server databases, but we learned recently (even though it's existed for a while) that you could use the.NET Core Identity Management with a MongoDB Database.


Now, when you create a new .NET Core Web Application, it's automatically configured with the right classes and setting for Identity Management using EntityFramework and SQL Server.

We have to do some tweaking here and there to get it ready for MongoDB. Let's take a look at how ...

  • Run dotnet new -t webin your terminal
  • Run dotnet restore; npm install; bower install in your terminal
  • Delete the entire Data/Migrations directory (you don't need it for MongoDB)
  • Remove these lines from project.json =>
    • "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0"
    • "Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview2-final", "imports": [ "portable-net45+win8+dnxcore50", "portable-net45+win8" ]}
    • "Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview2-final", "type": "build" } //dependencies
    • "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0"
    • "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1"
  • Install-Package Microsoft.AspNetCore.Identity.MongoDB or add in project.json and dotnet restore
  • Rename ApplicationUser to IdentityUser solution/project wide using find & replace or whatever tool you are comfortable with.
  • Delete the Models/IdentityUser.cs file
  • Delete the Data/ApplicationDbContext.cs file
  • Add using Microsoft.AspNetCore.Identity.MongoDB; to AccountController.cs and ManageController.cs files
  • Remove the following lines from Startup.cs =>
    • using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    • using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    • using Microsoft.EntityFrameworkCore;
    • using WebApplication.Data;
    • services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
    • services.AddIdentity<IdentityUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();
    • app.UseDatabaseErrorPage();
  • Prepend the following line(s) to the ConfigureServices method in Startup.cs
    • services.AddIdentityWithMongoStores(Configuration.GetConnectionString("DefaultConnection"));
  • In your appsettings.json file, set your ConnectionStrings property as shown below:
"ConnectionStrings": {
  "DefaultConnection": "mongodb://localhost:27017/local"
  • Add @using Microsoft.AspNetCore.Identity.MongoDB to your Views/_ViewImports.cshtml file
  • Run dotnet restore in your terminal

sample user data in mongodb

And there you have it

  • Chilezie Reginald Unachukwu
  • Ikechi Michael I.

Guide obtained from commit messages in this github repo

Show Comments