【问题标题】:Validating Against credentials Custom Users DB - IResourceOwnerPasswordValidator验证凭据自定义用户数据库 - IResourceOwnerPasswordValidator
【发布时间】:2017-02-13 01:16:17
【问题描述】:

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

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

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
    private IUserRepository _userRepository;

    public ResourceOwnerPasswordValidator(IUserRepository userRepository)
    {
        this._userRepository = userRepository;
    }

    public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
    {
        var isAuthenticated = _userRepository.ValidatePassword(context.UserName, context.Password);

       //code is omitted for simplicity                
    }
}

IProfileService:用于获取必要的声明

 public class ProfileService : IdentityServer4.Services.IProfileService
{
    public IUserRepository _userRepository;
    public ProfileService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
    public Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        //code is ommitted
    }

然后在 Startup 类的服务中添加必要的接口和依赖项。我还通过注入具有以下实现的登录服务修改了帐户控制器:

   public class LoginService
{
    private readonly IUserRepository _userRepository;
    public LoginService( IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
    public bool ValidateCredentials(string username, string password)
    {
        return _userRepository.ValidatePassword(username, password);
    }

AccountController“登录”操作通过调用验证凭据:

 if (_loginService.ValidateCredentials(model.Username, model.Password))
        {
           var user = _loginService.FindByUsername(model.Username);
           await HttpContext.Authentication.SignInAsync(user.Subject, user.Username);
             //other code is omitted  


               

在调试项目时,会调用 AccountContoller 的登录操作,然后是我的登录服务。验证用户凭据后,将显示同意页面,这会触发 ProfileService GetProfileDataAsync 方法。

但是,从未调用过 ResourceOwnerPasswordValidator。我是以正确的方式实现 LoginService 还是应该替换 IResourceOwnerPasswordValidation 注入的 IUserRepository,然后在 Login Service ValidateCredentails 中调用“ValidateAsync”方法?

如果是这样,那么将它传递给 LoginService 有什么好处,因为我已经在以这种方式验证用户了?

【问题讨论】:

  • 登录成功后生成的access_token中可以添加model.Password吗?

标签: asp.net-core thinktecture-ident-server identityserver4


【解决方案1】:

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

// Adds IdentityServer
services.AddIdentityServer()
    .AddResourceOwnerValidator<**PasswordAuthentication**>()

注意:确保不要将其添加到同样具有 .AddResourceOwnerValidator 功能的 AddIdentity 中。我花了一段时间。希望这可以帮助某人。

【讨论】:

    【解决方案2】:

    根据docs

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

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

    【讨论】:

    • 实现 OP 要求的正确方法是什么?在不使用 ASP.NET Identity 的情况下如何验证来自自定义用户数据库的凭据?我需要在幕后对 MS AD 做类似的事情。
    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多