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.
- Visual Studio Code (any other capable editor would do) or Visual Studio 2017
- DotNet core 1.0
- Working internet connect 😬
- Local setup of MongoDB
- 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.
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.
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.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; with
❌ Data Folder
Delete the entire Data folder.
Remove the following lines
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using IdentityWithMongo.Data;
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
Now, remove this app.UseDatabaseErrorPage(); from the Configure method.
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
At this point, you have to turn on your local MongoDB instance.
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 😂
That's all folks. As easy as it gets