• IdentityServer4实战:持久化 Client


    前言

    在前几篇的学习中,我们定义的四类 Client 都是存储在内存中的,通过 AddInMemoryClients(Startup.GetClients()) 的方式注入到 IDS4的服务中。本篇我们学习如何使用数据库或其他持久化方法存储和读取 Client 。

    自定义 ClientStore

    在  MicroShell.IdentityServer4.Server 项目新建 CustomerClientStore 类文件,代码如下:

    /// <summary>
        /// 自定义 客户端存储
        /// </summary>
        public class CustomerClientStore : IClientStore
        {
    
            private readonly List<Client> Clients = new List<Client>
            {
                        new Client
                        {
                            ClientId = "client1",
    
                            // 没有交互性用户,使用 clientid/secret 实现认证。
                            AllowedGrantTypes = GrantTypes.ClientCredentials,
    
                            // 用于认证的密码
                            ClientSecrets =
                            {
                                new Secret("secret".Sha256())
                            },
                            // 客户端有权访问的范围(Scopes)
                            AllowedScopes = { "api1" }
                        },
                        new Client
                        {
                            ClientId = "client2",
    
                            //  用户名 密码 模式
                            AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
    
                            // 用于认证的密码
                            ClientSecrets =
                            {
                                new Secret("secret".Sha256())
                            },
                            // 客户端有权访问的范围(Scopes)
                            AllowedScopes = { "api1" }
                        },
                        new Client
                        {
                            ClientId = "client3",
    
                            //  授权码 模式
                            AllowedGrantTypes = GrantTypes.Code,
    
                            RedirectUris = { "http://localhost:5001/test/index" },
    
                            // 是否需要确认授权,这个配置我们会在后面介绍,这里设置为 false
                            RequireConsent = false,
    
                            // 这个参数必须设置 为 false
                            RequirePkce = false,
    
                            // 用于认证的密码
                            ClientSecrets =
                            {
                                new Secret("secret".Sha256())
                            },
                            // 客户端有权访问的范围(Scopes)
                            AllowedScopes = {
                                "api1",
                                IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile
                            }
                        },
                        new Client
                        {
                            ClientId = "client4",
    
                            //  授权码 模式
                            AllowedGrantTypes = GrantTypes.Implicit,
    
                            RedirectUris = { "http://localhost:5001/test/index" }, // 认证成功后允许的回调地址
    
                            // 是否需要确认授权,这个配置我们会在后面介绍,这里设置为 false
                            RequireConsent = false,
    
                            // 用于认证的密码
                            ClientSecrets =
                            {
                                new Secret("secret".Sha256())
                            },
    
                            //AlwaysIncludeUserClaimsInIdToken=true,
    
                            //允许token通过浏览器 (必须 true)
                            AllowAccessTokensViaBrowser = true,
    
                            // 客户端有权访问的范围(Scopes)
                            AllowedScopes = {
                                "api1",
                                IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile
                            }
                        }
            };
    
            public async Task<Client> FindClientByIdAsync(string clientId)
            {
    
                // TODO: 这里可以从数据库或其他持久化设备获取 Client
    
                return Clients.FirstOrDefault(t => t.ClientId == clientId);
            }
        }

    ConfigureServices 注入 CustomerClientStore

     services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                    .AddInMemoryApiScopes(Startup.GetApiScopes())
                    .AddClientStore<CustomerClientStore>() // 注入自定义 ClientStore
                    .AddTestUsers(IdentityServerHost.Quickstart.UI.TestUsers.Users)
                    .AddInMemoryIdentityResources(Startup.GetIdentityResources())
                    ;
    

      

    本文转载自:https://limitcode.com/detail/606eaec2d9118c3cd4168797.html

  • 相关阅读:
    mysql允许远程访问
    ubuntu pip install MySQL-python mysql_config not found
    ubuntu 阿里云源
    V
    KMP算法之next函数解释(大量的反证法 和数学归纳法来袭)
    日常ACM题目
    F
    J
    中缀表达式求值 ,中缀表达转化为后缀表达式求值,
    数据结构
  • 原文地址:https://www.cnblogs.com/limitcode/p/14639518.html
Copyright © 2020-2023  润新知