当前位置:网站首页>[one by one series] identityserver4 (III) user name and password
[one by one series] identityserver4 (III) user name and password
2022-06-23 18:59:00 【DDGarfield】
Continue IdentityServer4, We introduced IdentityServer4 Realization OAuth2.0 One of the authorization methods is client certificate , Let's move on to OAuth2.0 Another way of authorization is password ,Resource Owner Password Credentials.
- post request
token?grant_type=password&username=USERNAME&password=PASSWORD&client_id=CLIENT_ID&client_secret=secret
From above url Of querystring Parameters can be seen , Here is the need to provide the user's user name and password , This is quite common in traditional projects
- web Background management system
- C/S client
1. to update IdentityServer
Because of the first part 【One by One series 】IdentityServer4( Two ) Use client credentials (Client Credentials) Protect API Resources have been created IdentityServer project , We just need IdentityServer4 Register users and add new clients in .
1.1 Registered users
There is no user involved in the client credentials , But the cipher is different , User name and password are required , Naturally, user data is needed . Of course, this content belongs to OpenID Connect 了 , Because it's about authentication .
We are Config.cs Add user data inside
public static List<TestUser> TestUsers =>
new List<TestUser>
{
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,"[email protected]"),
new Claim(JwtClaimTypes.EmailVerified,"true",ClaimValueTypes.Boolean),
new Claim(JwtClaimTypes.WebSite,"http://www.randyfield.cn"),
new Claim(JwtClaimTypes.FamilyName,"Randy"),
new Claim(JwtClaimTypes.Address,[email protected]" Chengdu high tech Zone, Sichuan Province ")
}
}
};
1.2 Register identity resources
The code is as follows :
public static IEnumerable<IdentityResource> IdentityResources =>
new IdentityResource[]
{
// You have to add , Otherwise, it will be invalid scope error
new IdentityResources.OpenId(),
new IdentityResources.Profile()
};
1.3 Register a new client
The code is as follows :
public static IEnumerable<Client> 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 }
},
};
Here is the client AllowedScopes except api resources , Additional users are specified Identity resources
2. Create client
Here we still use the client console program in the previous article , Just add code , Analog password Authorization
2.1 code - request Idisconvery endpoint
A little , It's the same as the first one
2.2 code - request 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;
}
RequestClientCredentialsTokenAsyncReplace withRequestPasswordTokenAsync- Request parameters
ClientCredentialsTokenRequestReplace withPasswordTokenRequest
- Request parameters
- The user name and password , Is in the
IdentityServerRegistered users - ClientId And ClientSecret I won't go into that
ScopePoint out api Resources and Identity resources
3. test
- start-up IdentityServer
cd .\IdentityServer\
dotnet run
- start-up webapi
cd .\webapi\
dotnet run
- use vs start-up client
3.1 obtain access-token
We go through http://jwt.calebb.net/ analysis
3.2 call api
3.3 Get identity information
call userinfo Endpoint , Get identity information
边栏推荐
- pmp考试需要备考多长时间?
- 【翻译】一种减小运动伪影的新方法基于AS-LMS自适应滤波器的PPG信号
- 微机原理第六章笔记整理
- 用户分析-AARRR模型(海盗模型)
- 【One by One系列】IdentityServer4(八)使用EntityFramework Core对数据进行持久化
- 重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
- 可编程数据平面(论文阅读)
- Machine learning jobs
- 又一家破产清算:那些在时代和资本裹挟下风雨飘摇的游戏公司
- Nanxin semiconductor rushes to the scientific innovation board: its annual revenue is RMB 980 million. Sequoia Xiaomi oppo is the shareholder
猜你喜欢

凸优化笔记
Database migration tool flyway vs liquibase (I)

涂鸦智能通过聆讯:拟回归香港上市 腾讯是重要股东

【翻译】一种减小运动伪影的新方法基于AS-LMS自适应滤波器的PPG信号

STM32 (VIII) -- PWM output

Docker搭建redis集群

吃顿饭的时间,学会simulink之BLDC基本原理

杰理之播 MP3 提示音功能【篇】

The yuan universe killer is coming! Xiao Zha offered 4 VR head displays to challenge the visual Turing test

Yaxiang spice listed on Shenzhen Stock Exchange: with a market value of 4billion, Dinglong Bohui and Yongyao investment are shareholders
随机推荐
Cloud security daily 220623: the red hat database management system has found an arbitrary code execution vulnerability and needs to be upgraded as soon as possible
在Microsoft Exchange Server 2007中安装SSL证书的教程
杰理之DAC 输出方式设置【篇】
浏览器跨域
从零开发小程序和公众号【第一期】
Shunted self attention | vit method for solving small target problems, which is derived from PVT and higher than PVT
Description of all shortcut keys in win11 system
高级计网笔记(三)
【One by One系列】IdentityServer4(八)使用EntityFramework Core对数据进行持久化
Browser cross domain
Principles of microcomputer Chapter 5 notes arrangement
CV-背景-简介
傑理之串口設置好以後打印亂碼,內部晶振沒有校准【篇】
【One by One系列】IdentityServer4(七)授权码流程原理之MVC
杰理之增加一个输入捕捉通道【篇】
高级计网笔记(四)
产品设计- 需求分析
高级计网笔记(五)
如何让一个list根据另一个list的顺序排序
(10) Binary tree