Articles → ASP .NET CORE → How To Create The Database Tables For The Identity Server In The Asp.Net Core

How To Create The Database Tables For The Identity Server In The Asp.Net Core






What Is An Identityserver?





Steps




  1. Create an ASP.NET core empty project
  2. Add a connection string in appsettings.json
  3. Add NuGet packages
  4. Add a new class inherited from IdentityDbContext class
  5. Add code to Program.cs
  6. Write commands in the Package Manager Console

Create An ASP.NET Core Empty Project




Picture showing selecting the asp.net core empty project as project template
Click to Enlarge


Add A Connection String In Appsettings.Json




Picture showing adding the connection string in appsettings.json file
Click to Enlarge




Add Nuget Packages




  1. IdentityServer4
  2. IdentityServer4.AspNetIdentity
  3. IdentityServer4.EntityFramework
  4. Microsoft.AspNetCore.Identity.EntityFrameworkCore
  5. Microsoft.EntityFrameworkCore
  6. Microsoft.EntityFrameworkCore.Design
  7. Microsoft.EntityFrameworkCore.SqlServer
Picture showing the list of packages installed in the project
Click to Enlarge




Add A New Class Inherited From Identitydbcontext Class




Picture showing the AspNetIdentityDbContext file created inside the Data folder
Click to Enlarge



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

namespace IdentityServerTablesDemo.Data
{
    public class AspNetIdentityDbContext : IdentityDbContext
    {
        public AspNetIdentityDbContext(DbContextOptions<AspNetIdentityDbContext> options) : base(options)
        {

        }
    }

}



Add Code To Program.Cs




using IdentityServerTablesDemo.Data;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// Code added for IdentityServer tables
var assembly = typeof(Program).Assembly.GetName().Name;
var defaultconnectionString = builder.Configuration["DefaultConnection"];

builder.Services.AddDbContext<AspNetIdentityDbContext>(options =>
options.UseSqlServer(defaultconnectionString, b =>
b.MigrationsAssembly(assembly)));

builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<AspNetIdentityDbContext>();

builder.Services.AddIdentityServer()
    .AddAspNetIdentity<IdentityUser>()
        .AddConfigurationStore(options =>
        {
            options.ConfigureDbContext = b => b.UseSqlServer(defaultconnectionString, opt => opt.MigrationsAssembly(assembly));
        })
        .AddOperationalStore(options =>
        {
            options.ConfigureDbContext = b => b.UseSqlServer(defaultconnectionString, opt => opt.MigrationsAssembly(assembly));
        })
        .AddDeveloperSigningCredential();

// Code added for IdentityServer tables ends here


var app = builder.Build();

// Code added for IdentityServer tables
app.UseIdentityServer();
// Code added for IdentityServer tables ends here

app.Run();



Write Commands In The Package Manager Console






Add-migration InitialIdentityServerMigration -c PersistedGrantDbContext

update-database -Context PersistedGrantDbContext

Add-migration AnotherMigration -c ConfigurationDbContext

update-database -Context ConfigurationDbContext

Add-migration AspNetIdentityDbContextMigration -c AspNetIdentityDbContext

update-database -Context AspNetIdentityDbContext




  1. The commands should be executed one by one
  2. InitialIdentityServerMigration, AnotherMigration and AspNetIdentityDbContextMigration are the migration names. You can give any name here

Output




Picture showing the list of identityserver tables created in sql server
Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Monday, November 21, 2022

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250