IdentityServer4 添加自定义登录验证

编程入门 行业动态 更新时间:2024-10-14 14:20:02

IdentityServer4 添加<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义登录验证"/>

IdentityServer4 添加自定义登录验证

一、自定义身份验证:ResourceOwnerPasswordValidator.cs

 public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator{public ResourceOwnerPasswordValidator(){}public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context){//根据context.UserName和context.Password与数据库的数据做校验,判断是否合法if (context.UserName == "wjk" && context.Password == "123"){context.Result = new GrantValidationResult(subject: context.UserName,authenticationMethod: "custom",claims: GetUserClaims());}else{//验证失败context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential");}}//可以根据需要设置相应的Claimprivate Claim[] GetUserClaims(){return new Claim[]{new Claim("userid", 1.ToString()),new Claim(JwtClaimTypes.Name,"dde"),new Claim(JwtClaimTypes.GivenName, "ssss"),new Claim(JwtClaimTypes.FamilyName, "ddddd"),new Claim(JwtClaimTypes.Email, "11111@qq"),new Claim(JwtClaimTypes.Role,"admin")};}}

 

services.AddIdentityServer().AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();

二、发现端点:/.well-known/openid-configuration

PostMan调用:

获取Token

Post:http://localhost:7000/connect/token

client_id:appClient
client_secret:secret
grant_type:password
username:wjk
password:123
scope:offline_access

刷新Token

Post:http://localhost:7000/connect/token

 

client_id:appClient
client_secret:secret
grant_type:refresh_token
refresh_token:785C83C87F2DE727FDB4B9397775BC90080CBBB4947C15D46FFD38E327E8342F

 

更多推荐

IdentityServer4 添加自定义登录验证

本文发布于:2024-03-23 20:22:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!