0
我正在使用一個asp.net mvc5項目,我希望我的用戶在他們登錄到我的網頁之前確認他們的電子郵件。我設法得到它,所以當用戶註冊用戶將會收到一封電子郵件。我也設法做到這一點,當用戶點擊電子郵件中的鏈接時,將在數據庫中得到確認。確認電子郵件不起作用
所以,問題的方法。我希望他們在可以登錄之前確認他們的電子郵件。 這是我的代碼,試圖實現這一點。
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.Username, model.Password); if (user != null)
{
if (user.EmailConfirmed == true)
{
await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Confirm Email Address.");
}
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
但是用戶無論如何都可以登錄,而無需確認他們的電子郵件。
這裏是我如何發送電子郵件。
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.Username };
user.Email = model.Email;
user.EmailConfirmed = false;
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
MailMessage m = new MailMessage(
new MailAddress("[email protected]", "Web Registration"),
new MailAddress(user.Email));
m.Subject = "Email confirmation";
m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail", "Account", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.stuff.net");
smtp.Credentials = new NetworkCredential("[email protected]", "passwordstuff");
smtp.EnableSsl = false;
smtp.Port = 8889;
smtp.Send(m);
return RedirectToAction("ConfirmEmail", "Account", new { Email = user.Email });
}
else
{
AddErrors(result);
}
}
問候。 Carlsson, 。
啊,我看你做了什麼沒有。這確實解決了我的問題,謝謝一堆。 –