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

时间:2015-03-25 15:25:27

标签: c# asp.net asp.net-mvc asp.net-identity owin

我的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));

用户存在,但我在重置方法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;
        }

2 个答案:

答案 0 :(得分:3)

如果问题是重复的电子邮件地址,您可以停用&#39; RequireUniqueEmail&#39; UserManager的属性:

C#示例

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

Visual Basic示例

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

答案 1 :(得分:0)

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

然后,您将UserValidator插入UserManager