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

    启用跨域

    在startup.cs中:
    在ConfigureServices类中添加

    services.AddCors(c =>  
    {  
        c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());  
    });
    

    在Configure类中添加

    app.UseCors(options => options.AllowAnyOrigin()); 
    
  • 相关阅读:
    [转载]oracle中的exists 和not exists 用法详解
    oracle中sql语句的优化(转帖)
    BizTalk Server 2010 使用 WCF Service [ 上篇 ]
    冒泡排序
    一起复习几何(4)
    手把手教你升级到 Mysql 5.5
    BizTalk Server 2010 映射器(Mapper) [ 上篇 ]
    基于OpenGL的渲染引擎
    BizTalk Server 2010 映射器(Mapper) [ 下篇 ]
    BizTalk Server 2010 映射器(Mapper) [ 中篇 ]
  • 原文地址:https://www.cnblogs.com/hwxing/p/12796781.html
Copyright © 2020-2023  润新知