MVC Blog: Authentication


Garry West
09/12/2024

I'd like to add security to my MvcBlog app, so I'm going to use ASP.NET Core Identity. I'm on version 8 of .NET. In the Microsoft docs, you may see references to AspNet.Identity.core - That is a totally different animal, you would only use if you were still on .NET framework.

At the powershell or dev command prompt, we add the ASP.NET Core code scaffolder:

You can add the required packages with the dotnet CLI or package manager:

Next we right-click on the project and Scaffold New Item. Pick Identity:

I left all the default UI files as is, no overrides. Created a new MvcBlogAuth context, and created a new MvcBlogUser class:

The Identity template creates a new Areas folder (you may have to refresh to see it):

Checking out program.cs, I needed to add a using statement for the new user class code inserted by Identity:

I also needed to set up a dependecy injection service for our MvcBlogAuth context, using the same connection string as MvcBlogContext:

And I added Razor page support:

I deleted out the MvcBlogAuthConnection connection string that was added to appsettings.json:

Build the project, add a migration and update the database, specifying the MvcBlogAuth context where our MvcBlogUser resides for each command:

In SQL Server Object Explorer, we see our new tables to support Identity:

identity , razor , blog