デフォルトのインターネットアプリケーションから始めて、MVC 4 アプリケーションを構築しています。私の目標は、ユーザーが Web サイトに登録するときに確認メールを送信することです。確認メールを送信することはできましたが、クリックしてもアカウントが確認されません。
注:登録アクションが混雑していることはわかっています。動作させるときに、ピースを別のファイルに分割します。
*これは私がやったことです: *
登録アクション
[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));
}
}
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");
}
また試した
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");
}
誰かがこれを機能させる方法を教えてもらえますか、またはおそらくこれを機能させる別の方法を提案してもらえますか??