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
- Create an ASP.NET core empty project
- Add a connection string in appsettings.json
- Add NuGet packages
- Add a new class inherited from IdentityDbContext class
- Add code to Program.cs
- Write commands in the Package Manager Console
Create An ASP.NET Core Empty Project
Click to Enlarge
Add A Connection String In Appsettings.Json
Click to Enlarge
Add Nuget Packages
- IdentityServer4
- IdentityServer4.AspNetIdentity
- IdentityServer4.EntityFramework
- Microsoft.AspNetCore.Identity.EntityFrameworkCore
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
Click to Enlarge
Add A New Class Inherited From Identitydbcontext Class
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
- The commands should be executed one by one
- InitialIdentityServerMigration, AnotherMigration and AspNetIdentityDbContextMigration are the migration names. You can give any name here
Output
Click to Enlarge