zoukankan      html  css  js  c++  java
  • net core 配置 IdentityServer4

    IdentityServer具有良好的扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制。 本快速入门介绍了如何配置IdentityServer以使用EntityFramework(EF)作为此数据的存储机制(而不是使用我们迄今为止使用的内存中实现)。

    IdentityServer4.EntityFramework组件

    有两种类型的数据需要持久化到数据库中。 首先是配置数据(资源和客户端),第二个是IdentityServer在使用时产生的操作数据(令牌,代码和用户的授权信息consents)。 这些存储采用接口进行建模,我们在IdentityServer4.EntityFramework Nuget包中提供这些接口的EF实现。

    添加 IdentityServer项目Nuget包的引用开始。

     

     使用SqlServer配置store

    下一步是在Startup.cs中ConfigureServices方法中的AddInMemoryClients,AddInMemoryIdentityResources和AddInMemoryApiResources进行替换。 我们将用这个代码替换它们:

    您可能需要将这些命名空间添加到文件中:

    using Microsoft.EntityFrameworkCore;
    using System.Reflection;
    

    上面的代码是对一个连接字符串进行硬编码,如果你愿意,你可以随意更改。 此外,对AddConfigurationStoreAddOperationalStore的调用是注册EF支持的存储实现。

    传递给这些API的“builder”回调方法是EF的机制,允许您为这两个存储中的每一个配置用于DbContextDbContextOptionsBuilder。 这就是我们的DbContext类可以用你想要使用的数据库提供程序来配置。 在这种情况下,通过调用UseSqlServer,我们正在使用SqlServer。 你也可以知道,这是提供连接字符串的地方。

    UseSqlServer中的“options”回调函数是配置定义EF迁移的程序集的方法。 EF需要使用迁移来定义数据库的Schema。

    添加迁移

    要创建迁移,请在IdentityServer项目目录中打开命令提示符。 在命令提示符下运行这两个命令:

    1.add-migration InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDb
    2.update-database -c PersistedGrantDbContext 3.
    add-migration InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb
    4.update-database -c ConfigurationDbContext

     执行情况应该如下:

     您现在应该在项目中看到一个〜/Migrations/IdentityServer文件夹。 这包含新创建的迁移的代码。

    初始化数据库

    现在我们已经添加了迁移,我们可以编写代码来从迁移中创建数据库。 我们还将使用我们在之前的快速入门中定义的内存配置数据对数据库进行种子处理。

    在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.IdentityResources)
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
    
            if (!context.ApiResources.Any())
            {
                foreach (var resource in Config.ApiResources)
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
    }
    

    然后我们可以从Configure方法调用它:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        // this will do the initial DB population
        InitializeDatabase(app);
    
        // the rest of the code that was already here
        // ...
    }
    

    现在,如果运行IdentityServer项目,则应创建数据库并使用快速入门配置数据进行种子插入。 您应该能够使用SQL Server Management Studio或Visual Studio来连接和检查数据。

     配置自己的用户数据表

     1.add-migration InitMyTable -c UserContext

      2.update-database -c UserContext

     

     

    登录密码校验

    运行程序

    配置成功,运行如下:

    您现在应该能够运行任何现有的客户端应用程序并登录,获取令牌并调用API - 全部基于数据库配置。

     获取Token调试如下:

     

    本文代码:https://gitee.com/gxiaopan/PlatformNetCore 

  • 相关阅读:
    io学习
    asp.net文件上传进度条研究
    asp.net页面中的Console.WriteLine结果如何查看
    谨慎跟随初始目的不被关联问题带偏
    android 按钮特效 波纹 Android button effects ripple
    安卓工作室 日志设置
    安卓工作室 文件浏览器 android studio File browser
    一个新的Android Studio 2.3.3可以在稳定的频道中使用。A new Android Studio 2.3.3 is available in the stable channel.
    新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial
    码云,git使用 教程-便签
  • 原文地址:https://www.cnblogs.com/Gxiaopan/p/13552037.html
Copyright © 2011-2022 走看看