Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 根据凭据验证自定义用户DB-IResourceOwnerPasswordValidator_Asp.net Core_Thinktecture Ident Server_Identityserver4 - Fatal编程技术网

Asp.net core 根据凭据验证自定义用户DB-IResourceOwnerPasswordValidator

Asp.net core 根据凭据验证自定义用户DB-IResourceOwnerPasswordValidator,asp.net-core,thinktecture-ident-server,identityserver4,Asp.net Core,Thinktecture Ident Server,Identityserver4,我使用Identity Server 4并实现了以下两个接口: IResourceOwnerPasswordValidator:用于根据自定义数据库验证用户凭据 公共类ResourceOwnerPasswordValidator:IResourceOwnerPasswordValidator { 专用IUserRepository\u用户存储库; 公共资源所有者PasswordValidator(IUserRepository userRepository) { 这是。_userReposito

我使用Identity Server 4并实现了以下两个接口:

IResourceOwnerPasswordValidator:用于根据自定义数据库验证用户凭据

公共类ResourceOwnerPasswordValidator:IResourceOwnerPasswordValidator
{
专用IUserRepository\u用户存储库;
公共资源所有者PasswordValidator(IUserRepository userRepository)
{
这是。_userRepository=userRepository;
}
公共任务ValidateSync(ResourceOwnerPasswordValidationContext)
{
var isAuthenticated=\u userRepository.ValidatePassword(context.UserName,context.Password);
//为了简单起见,省略了代码
}
}
IProfileService:用于获取必要的索赔

public class ProfileService:IdentityServer4.Services.IProfileService
{
公共IUserRepository\u用户存储库;
公共配置文件服务(IUserRepository userRepository)
{
_userRepository=userRepository;
}
公共任务GetProfileDataAsync(ProfileDataRequestContext上下文)
{
//代码已被修改
}
然后向Startup类中的服务添加了必要的接口和依赖项。我还通过注入具有以下实现的登录服务修改了Account Controller:

公共类登录服务
{
专用只读IUserRepository\u userRepository;
公共登录服务(IUserRepository userRepository)
{
_userRepository=userRepository;
}
public bool ValidateCredentials(字符串用户名、字符串密码)
{
返回_userRepository.ValidatePassword(用户名、密码);
}
AccountController“登录”操作通过调用以下命令验证凭据:

if(_loginService.ValidateCredentials(model.Username,model.Password))
{
var user=\u loginService.FindByUsername(model.Username);
等待HttpContext.Authentication.SignInAsync(user.Subject,user.Username);
//其他代码被省略
调试项目时,将调用AccountContoller的登录操作,然后调用我的登录服务。验证用户凭据后,将显示同意页面,这将触发ProfileService GetProfileDataAsync方法

但是,从未调用ResourceOwnerPasswordValidator。我是以正确的方式实现LoginService,还是应该替换由IResourceOwnerPasswordValidation注入的IUserRepository,然后调用登录服务ValidateRecordDetails中的“ValidateAsync”方法

如果是这样的话,既然我已经在用这种方式验证用户,那么将其传递给LoginService有什么好处呢

如果要使用OAuth 2.0资源所有者密码凭据 grant(又名password),您需要实现并注册 IResourceOwnerPasswordValidator接口:


由于您不使用密码授予,预期的行为是不调用
ResourceOwnerPasswordValidator
。对于您的情况,您不需要
ResourceOwnerPasswordValidator
实现。

我遇到了同样的问题。解决方案是按照您所做的那样做,但需要修改Startup.cs才能看到它

//添加IdentityServer
services.AddIdentityServer()
.AddResourceOwnerValidator()

注意:请确保您没有将其添加到同样具有
.AddResourceOwnerValidator
功能的
附加身份中。这花了我一些时间。希望这对其他人有所帮助。

实现OP要求的正确方法是什么?如何在不使用ASP.NET身份的情况下验证自定义用户数据库中的凭据?我不需要o在后台对MS广告执行类似操作。是否有任何方法将model.Password添加到登录成功后生成的access_令牌中?