public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } string username = UserHelpers.CreateUserName(model.Email); // 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(username, model.Password, model.RememberMe, shouldLockout : false); switch (result) { case SignInStatus.Success: var userid = UserManager.FindByEmail(model.Email).Id; //If user email has not been verified return view with message if (!UserManager.IsEmailConfirmed(userid)) { ViewBag.NotConfirmed = "A confirmation email was sent to " + model.Email + " but the email has not yet been confirmed. Please look for the confirmation" + " email in your inbox and click the provided link to confirm and log in."; ModelState.Clear(); return(View()); } else { 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)); } }
public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } string userid; try { userid = UserManager.FindByEmail(model.Email).Id; } catch (Exception e) { ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } //If user email has not been verified return view with message if (userid != null && !UserManager.IsEmailConfirmed(userid)) { ViewBag.NotConfirmed = "A confirmation email was sent to " + model.Email + " but the email has not yet been confirmed. Please look for the confirmation" + " email in your inbox and click the provided link to confirm and log in."; ModelState.Clear(); return(View()); } //Get Existing UserName string username = UserManager.FindByEmail(model.Email).UserName; //Update username only if it doesn't match the custom username conventions if (username.Contains('@')) { username = UserHelpers.CreateUserName(model.Email); } ; // 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(username, model.Password, model.RememberMe, shouldLockout : false); switch (result) { case SignInStatus.Success: //If no returnUrl when user logged in, send them to their profile //If user is Admin, send to school profile StuProfile profile = db.StuProfiles .Where(sp => sp.UserId == userid) .FirstOrDefault(); if (returnUrl == null && profile.FirstName != "Admin") { returnUrl = "/send/" + username; } else if (returnUrl == null && profile.FirstName == "Admin") { string schId = profile.SchoolId.ToString(); returnUrl = "/school/" + schId; } 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)); } }
public async Task <ActionResult> Register(RegisterViewModel model) { //----------------------------------------------- // Verify Domain of Email //----------------------------------------------- /*string schDomain = db.Schools * .Where(s => s.Id == model.SchoolId) * .Select(s => s.EmailDomain) * .Single(); * * if (!model.Email.Contains(schDomain)) * { * ViewBag.Schools = CreateSchoolList().AsEnumerable(); * ModelState.AddModelError("Email", "That domain is not approved by your school. " + "Please check for typos and ensure that you have selected the correct school. " + "If you believe you received this message in error, please contact your administrator."); + return View(model); + }*/ if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); //--------------------------------------------------- // Create username for custom url //--------------------------------------------------- user.UserName = UserHelpers.CreateUserName(model.Email); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); ViewBag.SentConf = "A confirmation email was sent to " + model.Email + ". Please look for the confirmation email in your inbox and click the provided link to confirm and log in."; ModelState.Clear(); ViewBag.Schools = CreateSchoolList().AsEnumerable(); return(View(model)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.ContainsKey("Email")) { ModelState["Email"].Errors.Clear(); } //----------------------------------------------- // Verify Domain of Email //----------------------------------------------- string schDomain = db.Schools .Where(s => s.Id == model.SchoolId) .Select(s => s.EmailDomain) .Single(); if (!model.Email.Contains(schDomain)) { ModelState.AddModelError("Email", "That domain is not approved by your school. " + "Please check for typos and ensure that you have selected the correct school. " + "If you believe you received this message in error, please contact your administrator."); ViewBag.Schools = new SchoolCollection(); return(View(model)); } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //--------------------------------------------------- // Verify Admin Key and Add to Role //--------------------------------------------------- string adminKey = WebConfigurationManager.AppSettings["AdminKey"]; bool isAdmin = false; if (model.AdminKey != null && model.AdminKey == adminKey) { var result1 = UserManager.AddToRole(user.Id, "Admin"); isAdmin = true; } //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //--------------------------------------------------- // Create username for custom url //--------------------------------------------------- user.UserName = UserHelpers.CreateUserName(model.Email); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); //--------------------------------------------------- // Create Student Profile & Add Placeholder Pic //--------------------------------------------------- StuProfile stuProfile = new StuProfile(user, model.SchoolId); if (isAdmin) { stuProfile.FirstName = "Admin"; stuProfile.LastName = "Admin"; } db.StuProfiles.Add(stuProfile); Upload placeholder = new Upload { File = "sm03162017profpic-placeholder.jpg", RefId = user.Id, TypeRef = "ProfPic" }; db.Uploads.Add(placeholder); db.SaveChanges(); //--------------------------------------------------- // Send Confirmation Email //--------------------------------------------------- string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string body = MailHelper.PopulateBody("Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); await UserManager.SendEmailAsync(user.Id, "Confirm your SendMe! account", body); ViewBag.SentConf = "A confirmation email was sent to " + model.Email + ". Please look for the confirmation email in your inbox and click the provided link to confirm and log in."; ModelState.Clear(); ViewBag.Schools = new SchoolCollection(); return(View(model)); } AddErrors(result); } // If we got this far, something failed, redisplay form ViewBag.Schools = new SchoolCollection(); return(View(model)); }