identityServer4

Posted hwxing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了identityServer4相关的知识,希望对你有一定的参考价值。

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页面

技术图片

技术图片

以上是关于identityServer4的主要内容,如果未能解决你的问题,请参考以下文章

IdentityServer4 快速上手

带有 IdentityServer4 和 oidc-client-js 的 ReactJS 错误:“没有响应代码”

IdentityServer4 刷新令牌为空

IdentityServer4实战 - 必须使用HTTPS问题解析

IdentityServer4 参考访问令牌有时作为 JWT 令牌传输

IdentityServer4 IdentityServer3.AccessTokenValidation