ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IdentityServer4实战:持久化 Client

2021-04-10 08:33:25  阅读:223  来源: 互联网

标签:实战 false ClientId secret Client new IdentityServer4 客户端


前言

在前几篇的学习中,我们定义的四类 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

标签:实战,false,ClientId,secret,Client,new,IdentityServer4,客户端
来源: https://www.cnblogs.com/limitcode/p/14639518.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有