Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a password reset action in my mvc controller with this code:

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

The user exists, but I'm having an error in the result object in reset method that says Name [email protected] is already taken.

Why is this happening? Could be because in the aspnetusers table in the database are two users with the same email and different tenantId? How can I fix this?

Update: AuthService is a protected property like this:

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

Where AuthenticationLogic constructor:

public AuthenticationLogic(IOwinContext owinContext) {
            this.owinContext = owinContext;
        }
like image 873
Phoenix_uy Avatar asked Dec 10 '25 01:12

Phoenix_uy


1 Answers

If the issue is a duplicate email address, you can disable the 'RequireUniqueEmail' property of the UserManager:

C# Example

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

Visual Basic example

manager.UserValidator = New UserValidator(Of ApplicationUser, Integer)(manager) With {
          .RequireUniqueEmail = False
        }
like image 198
Resource Avatar answered Dec 12 '25 15:12

Resource



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!