Identity Server 4资源拥有者密码认证控制访问API(二)

基于上一篇文章中的代码进行继续延伸,只需要小小的改动即可,不明白的地方可以先看看本人上一篇文章及源码Identity Server 4客户端认证控制访问API

一、 QuickStartIdentityServer4项目中Config.cs增加如下配置

// 定义客户端认证方式
        public static IEnumerable Clients => new[]
        {
            // 客户端认证
            new Client
            {
                ClientId="sample_client", // 客户端id
                ClientSecrets =
                {
                    new Secret("sample_client_secret".Sha256()) // 客户端秘钥

                },
                AllowedGrantTypes=GrantTypes.ClientCredentials, // 授权类型为客户端
                AllowedScopes={ "sample_api" } // 设置该客户端允许访问的api范围
            },
            // 资源拥有者认证
            new Client
            {
                ClientId="sample_pass_client", // 客户端id
                ClientSecrets =
                {
                    new Secret("sample_client_secret".Sha256()) // 客户端秘钥

                },
                AllowedGrantTypes=GrantTypes.ResourceOwnerPassword, // 授权类型为资源拥有者
                AllowedScopes={ "sample_api" } // 设置该客户端允许访问的api范围
            }
        };

 Identity Server 4资源拥有者密码认证控制访问API(二)

二、Client项目中增加模拟请求资源拥有者认证,其他代码不变

// 资源拥有者认证
            var tokenResponse = await client.RequestPasswordTokenAsync(
                    new PasswordTokenRequest
                    {
                        Address = disco.TokenEndpoint,
                        ClientId = "sample_pass_client",
                        ClientSecret = "sample_client_secret",
                        UserName="admin",
                        Password="123"
                    }
                );

 Identity Server 4资源拥有者密码认证控制访问API(二)

三、项目测试

1、’postman模拟请求 http://localhost:5001/connect/token 获取token

 Identity Server 4资源拥有者密码认证控制访问API(二)

2、使用token请求api

 Identity Server 4资源拥有者密码认证控制访问API(二)

3、Client项目模拟客户端请求

 Identity Server 4资源拥有者密码认证控制访问API(二)

学习链接:https://www.cnblogs.com/stulzq/p/7509648.html

Original: https://www.cnblogs.com/sportsky/p/16463755.html
Author: SportSky
Title: Identity Server 4资源拥有者密码认证控制访问API(二)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/608898/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球