簡體   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