Application authentication
.NET Core ASP.NET Core C# Entity Framework Core Powershell Visual Studio

How to Implement a .NET Core Identity Application with Entity Framework Core

Hi and welcome back to my blogs on development!

Today I will go over how you can add identity authentication to your .NET Core applications using EF Core and its Code First migration tool.

Firstly, in order to setup the identity authentication you will need to do the following setups:

Step 1 – Setup ASP.NET Identity in Startup.cs

Include the following namespaces into your Startup.cs:

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

In ConfigureService() method add the code shown:

services.AddIdentity<ApplicationUser, IdentityRole>()
  .AddEntityFrameworkStores<ApplicationDbContext>()
  .AddDefaultTokenProviders();

Step 2 – Usage of ASP.NET Identity in Service Classes

In any service classes that implement methods for identity related data access, include the following namespaces:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

Step 3 – Define the ApplicationUser entity

Creation of ApplicationUser for the ASP.NET Identity module:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
    }
}

The next steps are required in order to setup EF Migration, we will use EF Core to create the database based on the above definition.

This will require a connection string.

Step 4 – Setup Connection String

Open your appsettings.json and enter the ConnectionString as shown:

{
  "ConnectionStrings": {
    "AppDbContext": "Server=(localdb)\\mssqllocaldb;Database= EFCoreDemo;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
	…
}

Save the file.

Step 5 – Setup the Database Context

Create a \Data sub-folder under your project to store your EF data-related contexts and migrations.

Add a blank ApplicationDBContext.cs source file. Enter the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using BookLoan.Models;

namespace MyApp.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }

    }
}

Step 7 – Setup the Db Context to use SQL Server with the Connection String

Add the following lines within the ConfigureServices() method in Startup.cs:

services.AddDbContext<ApplicationDbContext>(
options =>
 options.UseSqlServer(
Configuration.GetConnectionString("AppDbContext"))
);

Step 8 – Create the Initial Database Based on the Db Context

Open PowerShell console within Visual Studio. Run the following command:

Add-Migration InitialCreate

Next, run the command:

Update-Database

When completed, open the SQL Explorer or SSMS. Open server localdb)\MSSQLLocalDB. Expand the tables and you should see the identity tables created as shown:

That’s all! Done!

In a future post I will explain how we can use the ASP.NET Core Identity to store user accounts and later how to add roles and claims.

Social media & sharing icons powered by UltimatelySocial