public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { 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: MethodAndFanction.setUserID(UserManager.FindByName(model.UserName).Id.ToString()); MethodAndFanction.setUserName(model.UserName.ToString()); 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 ActionResult LogOff() { MethodAndFanction.setUserID(null); MethodAndFanction.setUserName(null); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); return(RedirectToAction("Index", "Home")); ///RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR }
public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase Image) { if (ModelState.IsValid) { // Add image user if (Image != null) { model.userImage = new byte[Image.ContentLength]; Image.InputStream.Read(model.userImage, 0, Image.ContentLength); } var user = new ApplicationUser { UserName = model.userName, Email = model.Email, userAddress = model.userAddress, userBhtDay = model.userBthDay, userImage = model.userImage }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // MethodAndFanction.setUserID(UserManager.FindByEmail(model.Email).Id.ToString()); //MethodAndFanction.setUserName(UserManager.FindByEmail(model.Email).UserName.ToString()); MethodAndFanction.setUserName(user.UserName); MethodAndFanction.setUserID(UserManager.FindByName(user.UserName).Id.ToString()); RoleManager <IdentityRole> Role = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); //if (!Role.RoleExists("Admin")) //{ // Role.Create(new IdentityRole("Admin")); //} //else //{ // UserManager.AddToRole(MethodAndFanction.getUserId().ToString(), "Admin"); //} if (!Role.RoleExists("User")) { Role.Create(new IdentityRole("User")); } else { UserManager.AddToRole(UserManager.FindByName(user.UserName).Id.ToString(), "User"); } return(RedirectToAction("Index", "Posts")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }