افزودن تصدیق ایمیل به ASP.NET Identity در MVC 5
نویسنده: آرمین ضیاء
تاریخ: ۱۳۹۲/۱۰/۱۹ ۲۱:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class ApplicationUser : IdentityUser
{
public string Email { get; set; }
public string ConfirmationToken { get; set; }
public bool IsConfirmed { get; set; }
} private string CreateConfirmationToken()
{
return ShortGuid.NewGuid();
}
private void SendEmailConfirmation(string to, string username, string confirmationToken)
{
dynamic email = new Email("RegEmail");
email.To = to;
email.UserName = username;
email.ConfirmationToken = confirmationToken;
email.Send();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
string confirmationToken = CreateConfirmationToken();
var user = new ApplicationUser()
{
UserName = model.UserName,
Email = model.Email,
ConfirmationToken = confirmationToken,
IsConfirmed = false };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
SendEmailConfirmation(model.Email, model.UserName, confirmationToken);
return RedirectToAction("RegisterStepTwo", "Account");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
} private bool ConfirmAccount(string confirmationToken)
{
ApplicationDbContext context = new ApplicationDbContext();
ApplicationUser user = context.Users.SingleOrDefault(u => u.ConfirmationToken == confirmationToken);
if (user != null)
{
user.IsConfirmed = true;
DbSet<ApplicationUser> dbSet = context.Set<ApplicationUser>();
dbSet.Attach(user);
context.Entry(user).State = EntityState.Modified;
context.SaveChanges();
return true;
}
return false;
}
[AllowAnonymous]
public ActionResult RegisterConfirmation(string Id)
{
if (ConfirmAccount(Id))
{
return RedirectToAction("ConfirmationSuccess");
}
return RedirectToAction("ConfirmationFailure");
} [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 && user.IsConfirmed)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
} To: @ViewBag.To
From: YOURNAME@gmail.com
Subject: Confirm your registration
Hello @ViewBag.UserName,
Please confirm your registration by following the link bellow.
@Html.ActionLink(Url.Action("RegisterConfirmation", "Account", new { id = @ViewBag.ConfirmationToken }), "RegisterConfirmation", "Account", new { id = @ViewBag.ConfirmationToken }, null) @{ Layout = null; /* Overrides the Layout set for regular page views. */ } <system.net> <mailSettings> <smtp > <network host="smtp.gmail.com" userName="My Gmail Email" password="my Password" enableSsl="true" port="465" defaultCredentials="true"/> </smtp> </mailSettings> </system.net>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="armin.zia@gmail.com">
<network host="smtp.gmail.com" port="587" defaultCredentials="false" enableSsl="true" userName="YOUR-EMAIL" password="YOUR-PASSWORD" />
</smtp>
</mailSettings>
</system.net> The SMTP host was not specified.
<system.net> <mailSettings> <smtp from="my_gmail"> <network host="smtp.gmail.com" port="587" defaultCredentials="false" enableSsl="true" userName="my_gmail" password="mypassword" /> </smtp> </mailSettings> </system.net>
Html.ActionLink(".....Click Here for Confirmation.....",
"RegisterConfirmation", "Account", new { id = @ViewBag.ConfirmationToken }, null)