identityServer4(4)

AuthServer配置信息存数据库

打开之前创建的AuthServer项目

安装IdentityServer4.EntityFramework

dotnet add package IdentityServer4.EntityFramework --version 3.0.0

使用sqlite存储配置

安装

dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 3.1.0
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 3.1.0

在appsettings.json文件添加

"ConnectionStrings":{
    "DefaultConnection":"Data Source=authServer.db"
  },

在startup.cs文件中ConfigureServices类中代码修改如下:
添加

var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

将以下代码

builder.AddInMemoryIdentityResources(Config.Ids);
builder.AddInMemoryApiResources(Config.Apis);
builder.AddInMemoryClients(Config.Clients);

替换成

// 添加数据库中的配置数据(clients, resources)
builder.AddConfigurationStore(options => {
    options.ConfigureDbContext = builder => 
        builder.UseSqlite(Configuration.GetConnectionString("DefaultConnection"),
            sql => sql.MigrationsAssembly(migrationsAssembly));
});
// 添加来自数据库的操作数据(codes, tokens, consents)
builder.AddOperationalStore(options => {
    options.ConfigureDbContext = builder =>
        builder.UseSqlite(Configuration.GetConnectionString("DefaultConnection"),
            sql => sql.MigrationsAssembly(migrationsAssembly));
    // 启用自动令牌清除
    options.EnableTokenCleanup = true;
});

然后注释builder.AddDeveloperSigningCredential()

迁移

执行以下命令后,根目录多出以下迁移文件

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

数据库初始化

在startup.cs文件中新添加类

private void InitializeDatabase(IApplicationBuilder app)
{
    using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();

        var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
        context.Database.Migrate();
        if (!context.Clients.Any())
        {
            foreach (var client in Config.Clients)
            {
                context.Clients.Add(client.ToEntity());
            }
            context.SaveChanges();
        }

        if (!context.IdentityResources.Any())
        {
            foreach (var resource in Config.Ids)
            {
                context.IdentityResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }

        if (!context.ApiResources.Any())
        {
            foreach (var resource in Config.Apis)
            {
                context.ApiResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }
    }
}

在startup.cs中Configure类中添加

InitializeDatabase(app);

dotnet run运行AuthServer项目,在根目录会多出authServer.db,用工具打开可见生成好多表,且clients表中也有数据


将用户信息存数据库

原项目没有将用户信息存数据库,只使用了测试用户,所以要重建一个IdentityServer项目

新建IS4Server项目

运行dotnet new is4aspid -n IS4Server,当提示“seed”用户数据库时,选择“Y”

新建后的文件

接下来操作如下

然后按照之前的操作重做一遍,按照之前的操作重做一遍,按照之前的操作重做一遍

完成迁移,运行程序后,数据库比之前多出好几个表,如下

运行WebMvc、IS4Server项目,打开 http://localhost:5002/ 点击Privacy,跳转到登录页面,输入帐号:bob,密码:Pass123$ 后,跳转回Privacy页面

猜你喜欢

转载自www.cnblogs.com/hwxing/p/12796781.html