当前位置:网站首页>[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
边栏推荐
- VirtP4笔记
- CV convolution neural network
- NLP 论文领读|改善意图识别的语义表示:有监督预训练中的各向同性正则化方法
- 杰理之增加一个输入捕捉通道【篇】
- 涂鸦智能通过聆讯:拟回归香港上市 腾讯是重要股东
- 云安全日报220623:红帽数据库管理系统发现执行任意代码漏洞,需要尽快升级
- Machine learning jobs
- Various solutions to knapsack problems
- 对比学习(Contrastive Learning)综述
- Nanxin semiconductor rushes to the scientific innovation board: its annual revenue is RMB 980 million. Sequoia Xiaomi oppo is the shareholder
猜你喜欢

Learn the basic principles of BLDC in Simulink during a meal

从零开发小程序和公众号【第一期】
![[QT] Chapter 3 and 4: window components and layout management](/img/e6/fb35566c227c4a8e564594d40e4eab.png)
[QT] Chapter 3 and 4: window components and layout management

NetCF总结

物流服务与管理主要学什么

Borui data attends Alibaba cloud observable technology summit, and digital experience management drives sustainable development

Shunted Self-Attention | 源于 PvT又高于PvT,解决小目标问题的ViT方法

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

Halcon knowledge: contour operator on region (1)

【NOI2014】15.起床困难综合症【二进制】
随机推荐
Develop small programs and official account from zero [phase II]
Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]
【NOI2014】15.起床困難綜合症【二進制】
学习编程只需要这三条建议!
Product feedback mechanism
How to make a list sort according to the order of another list
各种解背包问题
杰理之.强制升级【篇】
矩阵分析笔记(一)
Jerry's dynamic switching vcomo modulation method [chapter]
Learn the basic principles of BLDC in Simulink during a meal
物流服务与管理主要学什么
Definition and model of indicators (complex indicators)
Taolue biology rushes to the scientific innovation board: the actual controllers with annual losses of more than 100 million are Zhang Dawei and his wife, who are American nationals
sed replace \tPrintf to \t//Printf
如何让一个list根据另一个list的顺序排序
云安全日报220623:红帽数据库管理系统发现执行任意代码漏洞,需要尽快升级
Task management of embedded development foundation (thread management)
高级计网笔记(九)
Use of stream streams