Migrating Identity to MongoDB (updated)

So, a lot has changed in DotNet core since I wrote this article with Michael and after this comment thread, I realized that I had to do an update to cover all that has changed on dotnet core so far. At least as much as we need to get through the article and set everything all up. In order to remain platform independent in this article, I will be covering both using VS 2017 and the CLI. All that said, letโ€™s begin.

Let's Begin

Prerequisites

  • Visual Studio Code (any other capable editor would do) or Visual Studio 2017
  • DotNet core 1.0
  • Working internet connect ๐Ÿ˜ฌ
  • Local setup of MongoDB

CLI

  • Because DotNet Coreโ€™s CLI creates your project based on the folder name, youโ€™d first have to create an appropriately named project folder. In my case, IdentityWithMongo.
mkdir IdentityWithMongo  
cd IdentityWithMongo  
  • Create a new DotNet core web project with identity.
dotnet new mvc -au Individual -f netcoreapp1.0  
  • Now launch Visual Studio Code and open the project folder you just created from within it.

  • Open IdentityWithMongo.cproj, go to the PackageReference section and remove the following lines. This will remove these packages from the projects as well.

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.0.3" PrivateAssets="All" />  
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.0.2" />  
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.0.2" />  
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.0.3" />  
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.0.3" PrivateAssets="All" />  
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0" PrivateAssets="All" />  
  • Now scroll to the DotNetCLIToolReference section and remove the following lines as well
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />  
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />  
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />  
  • Lets now add the Mongo DB Identity package in the .csproj file. This has a dependency on *MongoDB.Driver package and will be automatically installed.
<PackageReference Include="Microsoft.AspNetCore.Identity.MongoDB" Version="1.0.2" />  

Visual Studio 2017

  • Launch Visual 2017, Create a new DotNet core web project and set the authentication to Individual. Create a new dotnet core MVC project with Identity

  • Now open Nuget package manager for the project and uninstall Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 1.0.2, Microsoft.AspNetCore.Identity.EntityFrameworkCore 1.0.2, Microsoft.EntityFrameworkCore.DesignMicrosoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.SqlServer.Design and Microsoft.EntityFrameworkCore.Tools. Remove the packages that are not needed

  • Now still on the nuget package manager, click on the browse tab and search for Microsoft.AspNetCore.Identity.MongoDB and install the package. Close the tab when done.

Models/ApplicationUser.cs

Replace using Microsoft.AspNetCore.Identity.EntityFrameworkCore; with using Microsoft.AspNetCore.Identity.MongoDB;.

โŒ Data Folder

Delete the entire Data folder.

Startup.cs

Remove the following lines

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;  
using Microsoft.EntityFrameworkCore;  
using IdentityWithMongo.Data;  

add

using Microsoft.AspNetCore.Identity.MongoDB;  

Under the ConfigureServices method remove the following piece of codes.

// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>  
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()  
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

โ€ฆand add the following lines

 services.AddIdentityWithMongoStoresUsingCustomTypes<ApplicationUser, IdentityRole>(Configuration.GetConnectionString("DefaultConnection"));

Now, remove this app.UseDatabaseErrorPage(); from the Configure method.

Appsettings

Open appsettings.json and change the DefaultConnection's value to your local Mongo DB installation connection. In my case itโ€™s the default i.e. mongodb://localhost:27017/IdentityWithMongo

Conclusion

At this point, you have to turn on your local MongoDB instance.

mongod  

Using CLI, you might have to run a couple more commands to fetch the application dependencies and finally run your application.

dotnet restore; bower install;  
dotnet run;  

โ€ฆbut if using Visual Studio; just press play ๐Ÿ˜‚ Once again, And there you have it That's all folks. As easy as it gets

StarDownloadIssue

Show Comments