【问题标题】:Using email in token request with IdentityServer4在 IdentityServer4 的令牌请求中使用电子邮件
【发布时间】:2018-06-07 09:56:42
【问题描述】:

我正在使用带有 ASP.NET Core Identity 的 IdentityServer4 的 ROPC 流,我需要将用户名和密码发送到 /connect/token 端点以获取 access_token。

如何配置 IdentityServer4 以在 /connect/token 请求中接受用户名或电子邮件以及密码?

PS:asp.net 核心:2.0.2 身份服务器4:2.0.2

【问题讨论】:

    标签: asp.net-core identityserver4


    【解决方案1】:

    找到了这个解决方案:

    1) 将文件 IdentityServer4.AspNetIdentity/src/IdentityServer4.AspNetIdentity/ResourceOwnerPasswordValidator.cs 从https://github.com/IdentityServer/IdentityServer4.AspNetIdentity/blob/dev/src/IdentityServer4.AspNetIdentity/ResourceOwnerPasswordValidator.cs复制到您的项目中

    2) 修复 ValidateAsync 方法以通过电子邮件查找用户

            var user = await _userManager.FindByNameAsync(context.UserName);
            if (user == null) {
                user = await _userManager.FindByEmailAsync(context.UserName);
            }
    

    3) 向 IS4 添加验证器:

                .AddAspNetIdentity<AppUser>()
                .AddResourceOwnerValidator<Services.ResourceOwnerPasswordValidator<AppUser>>();
    

    4) 利润!

    【讨论】:

    【解决方案2】:

    您需要通过创建实现 IResourceOwnerPasswordValidator 接口的自定义 PasswordValidator 类来覆盖 ValidateAsync,如下所示。

      public class OwnerPasswordValidatorService : IResourceOwnerPasswordValidator
            {
                public UserManager<ApplicationUser> _userManager { get; }
                public OwnerPasswordValidatorService(UserManager<ApplicationUser> userManager)
                {
                    _userManager = userManager;
                }
        
                public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
                {
                    var user = await _userManager.FindByNameAsync(context.UserName);
        
                    if (user == null)
                    {
                        user = await _userManager.FindByEmailAsync(context.UserName);
        
                        if (user == null)
                        {
                          
    
      context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Username or password is incorrect");
                        return;
                    }
                }
    
                var passwordValid = await _userManager.CheckPasswordAsync(user, context.Password);
                if (!passwordValid)
                {
                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Username or password is incorrect");
                    return;
    
                }
    
                context.Result = new GrantValidationResult(subject: context.UserName, authenticationMethod: "custom");
            }
        }     
    

    然后为您的自定义类配置您的依赖项,如下所示。

    services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                     ...
                    .AddAspNetIdentity<ApplicationUser>()
                    .AddResourceOwnerValidator<OwnerPasswordValidatorService>() //here
                    .AddProfileService<ProfileService>();
    

    如果有人仍然无法使其工作,请告诉我。我很乐意提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2016-02-20
      • 2020-02-13
      • 2018-02-05
      • 1970-01-01
      • 2017-07-07
      相关资源
      最近更新 更多