繁体   English   中英

ASP.NET身份,重置密码抛出'名称已被采取'

[英]ASP.NET Identity, reset password throws 'Name is already taken'

我的mvc控制器中有一个密码重置操作,代码如下:

var user = AuthService.GetUser(command.IdUser);
if (user == null)
{
    return PartialView("_MessageFailed", "The user doesn't exists");
}
if (user.TenantId != command.IdWebsite)
{
    return PartialView("_MessageFailed", "You are not allowed to reset this user password");
}
var token = AuthService.GenerateUserPasswordToken(user.Id);
var result = AuthService.ResetPassword(user.Id, token, command.NewPassword);
if (result.Succeeded)
    return PartialView("_MessageSuccess", "The password has changed");
return PartialView("_MessageFailed", string.Join("<br>", result.Errors));

用户存在,但我在reset方法中的result对象中出现错误,表示Name user@domain.com is already taken.

为什么会这样? 可能是因为在数据库中的aspnetusers表中有两个用户具有相同的电子邮件和不同的tenantId? 我怎样才能解决这个问题?

更新:AuthService是受保护的属性,如下所示:

protected AuthenticationLogic AuthService
{
   get
     {
       return authService ?? new AuthenticationLogic(HttpContext.GetOwinContext());
     }
            private set { authService = value; }
}

AuthenticationLogic构造函数的位置:

public AuthenticationLogic(IOwinContext owinContext) {
            this.owinContext = owinContext;
        }

如果问题是重复的电子邮件地址,您可以禁用UserManager的“RequireUniqueEmail”属性:

C#示例

manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
    RequireUniqueEmail = false
};

Visual Basic示例

manager.UserValidator = New UserValidator(Of ApplicationUser, Integer)(manager) With {
          .RequireUniqueEmail = False
        }

是。 ASP.NET标识中的默认UserValidator不允许重复。 您将需要提供自己的UserValidator<TUser,TKey> (或UserValidator<Tuser> )的实现,并使用您自己的实现覆盖Validate(TUser item)方法,该方法也考虑TenantId

然后,您将UserValidator插入UserManager

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM