如何在MVC4中使确认链接激活帐户?

Kom*_*gem 4 c# security email-confirmation ef-code-first asp.net-mvc-4

我正在构建一个MVC 4应用程序,从默认的Internet应用程序开始.我的目标是在用户注册到网站时向用户发送确认电子邮件,我已设法发送确认电子邮件,但是当我点击它时,帐户未被确认.

注意:我知道注册操作很拥挤,当我使其工作时,我会将各个文件分开.

*这就是我所做的:*

注册行动

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            try //CreateUserAndAccount
            {
                var token = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, true);
                if (token != null)
                {
                    var hosturl =
                        System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +
                        "/Account/ConfirmAccount?token=" + token;

                    var confirmationLink = string.Format("<a href=\"{0}\">Clink to confirm your registration</a>",
                                                         hosturl);




                    var message = new MailMessage("komengem@gmail.com", model.UserName)
                    {
                        Subject = "Please confirm your email",
                        Body = confirmationLink
                    };

                    var client = new SmtpClient();
                    client.EnableSsl = true;
                    client.Send(message);
                }

                TempData["message"] = string.Format(
                    "Thank you for registering. An email has been sent to {0}. " +
                    "Please check your email and use the enclosed link to finish registration.", model.UserName);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
Run Code Online (Sandbox Code Playgroud)

ConfirmAcount操作

public ActionResult ConfirmAccount(string ID)
    {
        //return View("ConfirmAccount");
        var confirmationToken = Request["token"];
        if (!String.IsNullOrEmpty(confirmationToken))
        {
            var user = WebSecurity.CurrentUserName;
            WebSecurity.ConfirmAccount(confirmationToken);
            WebSecurity.Login(user, null);
            return View("Welcome");
        }

        TempData["message"] = string.Format(
                    "Your account was not confirmed, please try again or contact the website adminstrator.");
        return RedirectToAction("Index", "Home");            
    }
Run Code Online (Sandbox Code Playgroud)

也试过

public ActionResult ConfirmAccount(string ID)
    {
        if (string.IsNullOrEmpty(ID) || (!Regex.IsMatch(ID,
                        @"[0-9a-f]{8}\-([0-9a-f]{4}\-){3}[0-9a-f]{12}")))
        {
            TempData["tempMessage"] =
                "The user account is not valid. Please try clicking the link in your email again.";
            return View();
        }
        MembershipUser user = Membership.GetUser(new Guid(ID));

        if (!user.IsApproved)
        {
            user.IsApproved = true;
            Membership.UpdateUser(user);
            FormsAuthentication.SetAuthCookie(user.UserName, false);
            return RedirectToAction("Login");
        }
        WebSecurity.Logout();
        TempData["tempMessage"] = "You have already confirmed your email address... please log in.";
        return RedirectToAction("Login");
    }      
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何使这项工作,或者可能建议一种不同的方式来使这项工作?

Fre*_*rik 7

我鼓励您使用Postal,这是一个简单易用的电子邮件创建包.它可以作为nuget-package使用.

还可以看一下关于这个主题的Kevin Junghans博文!邮件确认