1

我正在尝试在我的网站的电子邮件注册期间编写用于电子邮件确认的代码。我有一些自定义的用户模型,类似于常规用户代码。

发送电子邮件确认的代码在Register操作中:

userService.InsertUser(user);

var appuser = new ApplicationUser(user); //{ UserName = model.Email, Email = model.Email };
appuser.UserName = model.Email;
//var result = await UserManager.CreateAsync(appuser, model.Password);
//if (result.Succeeded)
//{
//    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

//    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
//    // Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(appuser.Id);
//string htmlcode = HttpUtility.UrlEncode(code);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = appuser.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(appuser.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

return RedirectToAction("RegisterComplete");

从代码中,我得到callbackurlhttp://localhost:63599/Account/ConfirmEmail?userId=22&code=C3mfUtYqGzVCsrCnE5VDzC0wfsbgP6lMSVgqFk25kym3uGh0%2B%2Bltqyh0VRim1ulbwSRHBzAgUCJ1WceipbRcErddmNJqzkksZN50QO%2FqTRprpgKcW19wX33QtftwCh7zUz%2B01aghCdg8jZ8Ff%2FNVfRvbjyIW0wavN0Ueq3xl6jBmrv%2BrnbtuKLJO%2BuFE2zHF

当我在浏览器中输入代码时,它将运行ConfirmEmailAccountController 中的操作:

[AllowAnonymous]
    public async Task<ActionResult> ConfirmEmail(string userId, string code)
    {
        if (userId == null || code == null)
        {
            return View("Error");
        }
        var result = await UserManager.ConfirmEmailAsync(userId, code);
        //if (result.Succeeded)
        //{
        //    UserService userService = new UserService();
        //    long uid = long.Parse(userId);
        //    User user = userService.GetBy(u => u.UserID == uid).FirstOrDefault();
        //    user.IsVerified = true;
        //    userService.Update(user);
        //}
        return View(result.Succeeded ? "ConfirmEmail" : "Error");
    }

但是result.Succeededfalse。和result.Error[0] : Name cannot be null or empty。在这种情况下,我找不到有关此错误的更多信息,因此我很困惑。

如果我更改确认码,我会得到result.Error[0]: Invalid token,这让我觉得至少我的令牌是正确的。但我不知道这个错误在抱怨什么。

任何帮助/指针将不胜感激。

4

0 回答 0