Search code examples
asp.net-coreasp.net-identityblazor-server-side

ASP.NET Identity library - how change email, password?


In the ASP.NET Core Identity library, how do I give the user the option of changing their email & password?

If necessary, I can handle the email change myself directly updating the Identity database. And then generating the link to email to the user to confirm. But if the library has the page to do that, I'd prefer to use it.

And the password, I need the library because I don't know how to set the hash for a password. So where/how is the page for that change?


Solution

  • Yes, the identity contains the default page which contains the confirmemailchange and change the password(await _userManager.ChangeEmailAsync(user, email, code);,_userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword)).

    You could use VS add new scaffolded item to add it.

    enter image description here

    enter image description here

    After adding it ,you could find it use the usermanager to change the email or password.

    Normally, we will generate the Email change token and use ConfirmEmailChange

    var code = await _userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);
    var callbackUrl = Url.Page(
        "/Account/ConfirmEmailChange",
        pageHandler: null,
        values: new { userId = user.Id, email = model.NewEmail, code = code },
        protocol: Request.Scheme);
    await _emailSender.SendEmailAsync(
        model.NewEmail,
        "Confirm your email change",
        $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");