简体   繁体   English

使用 UserManager 更改用户的 email

[英]Using UserManager to change the email of a user

I am trying to implement a feature to change the email of a user.我正在尝试实现一个功能来更改用户的 email。 Asp net core identity by default allows the user to change an email however it requires a confirmation link.默认情况下,Asp 网络核心标识允许用户更改 email,但它需要确认链接。 I was wondering if it was possible to not use the confirmation link and just edit the function to update the email of the user with the new email.我想知道是否可以不使用确认链接,只需编辑 function 以使用新的 email 更新用户的 email。 Thanks in advance提前致谢

I have tried doing我试过做

 await _userManager.ChangeEmailAsync(user, Input.NewEmail, code);

and

var changingser = _userManager.Users.First(u => u.Email.Equals(email));
changingser.Email = Input.NewEmail;

Which did not seem to work这似乎不起作用

Email.cshtml: Change Email function Email.cshtml:更改 Email function

    public async Task<IActionResult> OnPostChangeEmailAsync()
    {
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
        }

        if (!ModelState.IsValid)
        {
            await LoadAsync(user);
            return Page();
        }

        var email = await _userManager.GetEmailAsync(user);
        if (Input.NewEmail != email)
        {
            var userId = await _userManager.GetUserIdAsync(user);
            var code = await _userManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail);
            code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

            await _userManager.ChangeEmailAsync(user, Input.NewEmail, code);

/*                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmailChange",
                    pageHandler: null,
                    values: new { userId = userId, email = Input.NewEmail, code = code },
                    protocol: Request.Scheme);*/

                //await _emailSender.SendEmailAsync(
                //    Input.NewEmail,
                //    "Confirm your email",
                //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            StatusMessage = "Email has been changed";
            return RedirectToPage();
        }

        StatusMessage = "Your email is unchanged.";
        return RedirectToPage();
    }

Edit: Failed invalid token?编辑:失败的无效令牌?

在此处输入图像描述

Yes, you can.是的你可以。

Use SetEmailAsync to see the user email.使用 SetEmailAsync 查看user email。
This will still require the email to be 'confirmed' so generate a token, then immediately confirm the new email address.这仍然需要“确认” email,因此生成一个令牌,然后立即确认新的 email 地址。

Something like:就像是:

await _userManager.SetEmailAsync(user, Input.NewEmail);

var token = await _userManager.GenerateEmailConfirmationTokenAsync(existingUser);

await _userManager.ConfirmEmailAsync(existingUser, token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM