IdentityServer4资源拥有者密码认证控制访问API

博客 动态
0 231
羽尘
羽尘 2022-07-12 00:01:39
悬赏:0 积分 收藏

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

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

 

 

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

// 定义客户端认证方式        public static IEnumerable<Client> 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范围            }        };

 

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

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

 

 三、项目测试

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

 

 2、使用token请求api

 

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

 

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

 

posted @ 2022-07-11 23:29 SportSky 阅读(13) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员