博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【One by One系列】IdentityServer4(三)使用用户名和密码
阅读量:4034 次
发布时间:2019-05-24

本文共 4274 字,大约阅读时间需要 14 分钟。

继续介绍IdentityServer4,我们上篇介绍了IdentityServer4实现OAuth2.0的授权方式之一的客户端凭证,接下来我们继续介绍OAuth2.0的另外一种授权方式密码式,Resource Owner Password Credentials。

  • post请求token?grant_type=password&username=USERNAME&password=PASSWORD&client_id=CLIENT_ID&client_secret=secret

从上面url的querystring参数就可以看出来,这里主要就是需要提供用户的用户名和密码,这个在传统的项目还是比较常见

  • web后台管理系统

  • C/S客户端

1.更新IdentityServer

由于上篇【One by One系列】IdentityServer4(二)使用客户端凭证(Client Credentials)保护API资源已经创建的IdentityServer项目,我们只需要IdentityServer4中注册用户和添加新的客户端。

1.1 注册用户

客户端凭证是没有用户参与的,但是密码式不同,需要用户输入用户名和密码,自然就需要用户数据。当然这块内容就属于OpenID Connect了,因为这跟身份认证相关。

我们在Config.cs里面增加用户数据

public static List
 TestUsers =>            new List
            {                new TestUser()                {                    SubjectId="1",                    Username="admin",                    Password="admin123456!",                    Claims=                    {                         new Claim(JwtClaimTypes.Name,"RandyField"),                        new Claim(JwtClaimTypes.GivenName,"Randy"),                        new Claim(JwtClaimTypes.FamilyName,"Field"),                        new Claim(JwtClaimTypes.Email,"xxx@qq.com"),                        new Claim(JwtClaimTypes.EmailVerified,"true",ClaimValueTypes.Boolean),                        new Claim(JwtClaimTypes.WebSite,"http://www.randyfield.cn"),                        new Claim(JwtClaimTypes.FamilyName,"Randy"),                        new Claim(JwtClaimTypes.Address,$@"四川省成都市高新区")                    }                              }            };

1.2 注册身份资源

代码如下:

public static IEnumerable
 IdentityResources =>            new IdentityResource[]            {       //必须要添加,否则报无效的scope错误                new IdentityResources.OpenId(),                new IdentityResources.Profile()            };

1.3 注册新客户端

代码如下:

        public static IEnumerable
 Clients =>            new Client[]            {                 new Client                    {                        ClientId = "client app",                        // no interactive user, use the clientid/secret for authentication                        AllowedGrantTypes = GrantTypes.ClientCredentials,                        // secret for authentication                        ClientSecrets =                        {                            new Secret("secret-123456".Sha256())                        },                        // scopes that client has access to                        AllowedScopes = { "api1" }                    },                         //Resource Owner Password Credentials Client                 new Client                     {                        ClientId="client pwd",                        AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,                        ClientSecrets=                         {                            new Secret("secret-654321".Sha256())                         },                        AllowedScopes={ "api1",                         IdentityServerConstants.StandardScopes.OpenId,                         IdentityServerConstants.StandardScopes.Profile }                     },            };

这里客户端AllowedScopes除了api资源,还额外指定了用户Identity资源

2.创建客户端

这里我们依然使用上篇的中的客户端控制台程序,只是增加代码,模拟密码式授权

2.1 编码-请求Idisconvery endpoint

略,与上篇相同

2.2 编码-请求access token

            // request token            var tokenResponse1 = await client.RequestPasswordTokenAsync(new PasswordTokenRequest            {                Address = disco.TokenEndpoint,                ClientId = "client pwd",                //ClientId = "client",                ClientSecret = "secret-654321",                Scope = "api1 openid profile",                UserName= "admin",                Password= "admin123456!"            });            if (tokenResponse1.IsError)            {                Console.WriteLine(tokenResponse1.Error);                return;            }
  • RequestClientCredentialsTokenAsync更换为RequestPasswordTokenAsync

    • 请求参数ClientCredentialsTokenRequest更换为PasswordTokenRequest

  • 其中的用户名和密码,就是在IdentityServer注册的用户

  • ClientId与ClientSecret就不赘述了

  • Scope指明了api资源和Identity资源

3.测试

  • 启动IdentityServer

cd .\IdentityServer\dotnet run
  • 启动webapi

cd .\webapi\dotnet run
  • 用vs启动client

3.1 获取access-token

我们通过http://jwt.calebb.net/解析

3.2 调用api

3.3 获取身份信息

调用userinfo端点,获取身份信息

长按二维码关注

点外卖,先领券

转载地址:http://ududi.baihongyu.com/

你可能感兴趣的文章
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>