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

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 [email protected] is already taken.
为什么会这样?
可能是因为在数据库的 aspnetusers 表中有两个用户使用相同的电子邮件和不同的租户 ID?我怎样才能解决这个问题?

更新:
AuthService 是一个 protected 属性,如下所示:
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
        }

关于c# - ASP.NET Identity,重置密码抛出 'Name is already taken',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29259821/

10-11 02:45