public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false); switch (result) { case SignInStatus.Success: this.loggedInUser = this._dbContext.Users.FirstOrDefault(x => x.Email.Equals(model.Email)); this.loggedInUser.LastLogin = DateTime.Now; _dbContext.SaveChanges(); return(RedirectToAction("Index", "Configuration")); case SignInStatus.RequiresVerification: return(RedirectToAction("NotVerficated", "Account")); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } }
// send verification email private void SendVerificationEmail(SnowwhiteUser user) { SnowwhiteUser userForMail = loggedInUser ?? user; string body = "<img src='~/Content/Images/snowwhite-logo48.png'/>"; body += "<h2 style='color: #2196F3; font-weight: 300; font-size: 6em; 2.92rem;'>Hello " + userForMail.FirstName + " " + userForMail.LastName + "!</h2>"; //body += "<br /><a href = '" + Url.Action("Verify", "Account", new { id = userForMail.Id }) + "'>Click here to activate your account.</a>"; body += "<br /><a href = 'http://snowwhite-configurationpage.azurewebsites.net/Account/Verify?id=" + userForMail.Id + "'>Click here to activate your account.</a>"; body += "<br /><br/> Thank you and have fun with enjoying your smart mirror!"; body += "<br /><br/><span style='color: #2196F3; font-weight: 100; font-size: 3em;'>Your Snowwhite-Team</span>"; try { MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailAddress"]); mail.To.Add(userForMail.Email); mail.Subject = "Verificatation for " + userForMail.FirstName + " " + userForMail.LastName; mail.Body = body; mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.UTF8; mail.IsBodyHtml = true; smtpClient.Send(mail); } catch (Exception e) { } }
public async Task <ActionResult> Register(RegisterViewModel model) { // if model is valid if (ModelState.IsValid) { // new user // this data have to be set by hand. The rest will be filled automatically var user = new SnowwhiteUser { FirstName = model.FirstName, LastName = model.LastName, Thumbnail = SaveFile(model.Thumbnail), RegistrationDate = DateTime.Now, UserName = model.Email, Email = model.Email }; // user will be created and saved in the database | identity entity framework save this => no context.SaveChanges() needed var result = await UserManager.CreateAsync(user, model.Password); // result = success? if (result.Succeeded) { SendVerificationEmail(user); return(RedirectToAction("Index", "Home")); } AddErrors(result); } return(View(model)); }
// on the verification mail the user will be redirected to this method public ActionResult Verify(string id) { SnowwhiteUser user = _dbContext.Users.FirstOrDefault(x => x.Id.Equals(id)); user.EmailConfirmed = true; _dbContext.SaveChanges(); return(RedirectToAction("Index", "Configuration")); }
// send email to reset the old password public async Task <JsonResult> SendResetPasswordMail(string email) { SnowwhiteUser userForMail = _dbContext.Users.FirstOrDefault(x => x.Email.Equals(email)); string body = "<img src='~/Content/Images/snowwhite-logo48.png'/>"; body += "<h2 style='color: #2196F3; font-weight: 300; font-size: 6em;'>Hello " + userForMail.FirstName + " " + userForMail.LastName + "!</h2>"; //body += "<br /><a href = '" + Url.Action("ResetPassword", "Account", new { id = userForMail.Id, forgot = true }) + "'>Click here to reset your password.</a>"; body += "<br /><a href = 'http://snowwhite-configurationpage.azurewebsites.net/Account/ResetPassword?id=" + userForMail.Id + "'>Click here to reset your password.</a>"; body += "<br /><br/><span style='color: #2196F3; font-weight: 100; font-size: 3em;'>Your Snowwhite-Team</span>"; using (MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["EmailAddress"], userForMail.Email)) { MailAddress sender = new MailAddress(ConfigurationManager.AppSettings["EmailAddress"], "Snowwhite-Team", Encoding.UTF8); mm.Subject = "Resetting for " + userForMail.FirstName + " " + userForMail.LastName; mm.Body = body; mm.BodyEncoding = Encoding.UTF8; mm.IsBodyHtml = true; smtpClient.SendAsync(mm, userForMail.Id); } return(Json(await Task.FromResult(0), JsonRequestBehavior.AllowGet)); }