IdentityServer4密码模式接入现有用户数据表

博客介绍了IdentityServer的接入步骤,包括创建web项目并引入Identityserver4的nuget包、新建类实现IResourceOwnerPasswordValidator接口等,还说明了在api项目中配置好IdentityServer后,通过Post请求特定地址获取token,且设置token时要加Bearer前缀。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体接入identityserver请看文档,这里只简单列举部分步骤
1.创建一个web项目,引入Identityserver4的nuget包
2.新建一个类,实现IResourceOwnerPasswordValidator接口
···csharp
public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
VerifyUserInputDto inputDto = new VerifyUserInputDto
{
UserName = context.UserName,
Password = context.Password
};
var verifyResult = await _userService.VerifyUserPasswordAsync(inputDto);
if (verifyResult.isSuccess)
{
context.Result = new GrantValidationResult(verifyResult.userInfo.UserId.ToString(), OidcConstants.AuthenticationMethods.Password);
}
else
{
//验证失败
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential");
}
}

大致就是读取数据库数据,与context.username ,password,进行比对,一致则通过,不一致就是失败。
3.Starpup中增加
···csharp
services.AddIdentityServer()
                  .AddInMemoryApiResources(你定义的资源)
                  .AddInMemoryClients(你定义的客户端)
                  .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>() //主要是user这里替换我们自己的服务
                  .AddDeveloperSigningCredential(); // rsa密钥,自动生产一个临时的。

4.api项目中 配置好identityserver ,Post请求 identityserver 下connect/token 这个地址 获取token, 记得在设置token的时候加上 Bearer 前缀 否则token是无效的!!

转载于:https://www.cnblogs.com/zzqvq/p/11180926.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值