夫天地者,万物之逆旅;光阴者,百代之过客。而浮生若梦,为欢几何?
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())
                ;


作者:暗夜余晖

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

0

支持

0

反对

posted @2021-4-6  拜读(694)

评论列表

评论内容:



喜欢请打赏

支付宝 微信

请放心支付