public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { var isInRole = await BaseHepler.IsActive(model.UserName); if (!isInRole) { ModelState.AddModelError("UserName", model.UserName + " is Not Exist"); } if (!ModelState.IsValid) { return(View(model)); } // 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: BaseHepler.SetCookie(model.UserName); 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)); } }
private void RemoveCookie() { BaseHepler.LogOutTime(User.Identity.GetUserId <int>()); var cookie = new ManageCookie(); cookie.RemoveCookie("APPUSER"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-364)); Response.Cache.SetNoStore(); Session.Clear(); Session.Abandon(); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber, FullName = model.Name, OrgId = 1, ImageUrl = "/Images/user.png", AgentId = PcUniqueNumber.GetUserAgentInfo, Created = DateTime.UtcNow.ToLong(), Expired = DateTime.UtcNow.AddYears(5).ToLong(), Modified = DateTime.UtcNow.ToLong(), IsActive = true, InVacation = false, }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { UserManager.AddToRole(user.Id, "SUBSCRIBER"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); RemoveCookie(); BaseHepler.SetCookie(model.Email); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 //Send an email with this link //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "UserAuth", 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>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }