public async Task <ActionResult> Login(LoginModel details, string returnUrl) { if (ModelState.IsValid) { AppUser user = await UserManager.FindAsync(details.Name, details.Password); if (user == null) { ModelState.AddModelError("", "Invalid name or password."); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect(returnUrl)); } } return(View(details)); }
public async Task <ActionResult> Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { ApplicationUser user = await UserManager.FindAsync(model.Name, model.Password); if (user == null) { ModelState.AddModelError("", "Invalid username or password"); } else { try { var claimsIdentity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); claimsIdentity.AddClaims(LocationClaimsProvider.GetClaims(claimsIdentity)); claimsIdentity.AddClaims(ClaimsRoles.CreateRolesFromClaims(claimsIdentity)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, claimsIdentity); } catch (System.Exception ex) { throw; } return(Redirect(returnUrl)); } } ViewBag.returnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> Login(LoginViewModel details, string returnUrl) { AppUser user = await UserManager.FindAsync(details.Name, details.Password); if (user == null) { ModelState.AddModelError("", "Некоректне ім'я або пароль."); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); //вычитать доп утверждения ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); //доп сконструированные утверждения ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect(returnUrl)); } return(View(details)); }
public async Task <ActionResult> Login(LoginViewModel details, string returnUrl) { AppUser user = await UserManager.FindAsync(details.Name, details.Password); if (user == null) { ModelState.AddModelError("", "Некорректное имя или пароль."); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); // Мы добавили только эту строку ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect(returnUrl)); } return(View(details)); }
[ValidateAntiForgeryToken] //用来防止CSRF跨站请求伪造 public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { //根据用户名和密码查询到用户 MyUser user = await UserManager.FindAsync(model.Name, model.Password); if (user == null) { ModelState.AddModelError("", "无效的用户名或密码"); } else { var claimsIdentity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); //将自定义声明传入到ClaimsIndentity claimsIdentity.AddClaims(LocationClaimsProvider.GetClaims(claimsIdentity)); claimsIdentity.AddClaims(ClaimsRoles.CreateRolesFromClaims(claimsIdentity)); AuthManager.SignOut(); //AuthenticationProperties 对象和ClaimsIdentity 对象,AuthticationProperties 有众多属性,我在这儿只设置IsPersistent=true 。 //意味着Authentication Session 被持久化保存,当开启新Session 时,该用户不必重新验证 AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, claimsIdentity); //登录成功重定向到原请求地址 return(Redirect(returnUrl)); } } ViewBag.returnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> LoginAsync(LoginModel details, string returnUrl) { if (ModelState.IsValid) { var userManager = HttpContext.GetAppUserManager(); var user = await userManager.FindAsync(details.Name, details.Password).ConfigureAwait(false); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid name or password"); } else { var identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie) .ConfigureAwait(false); identity.AddClaims(LocationClaimsProvider.GetClaims(identity)); identity.AddClaims(ClaimsRoles.CreateRolesFromClaims(identity)); var authManager = HttpContext.GetAuthManager(); authManager.SignOut(); authManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(Redirect(returnUrl)); } } ViewBag.ReturnUrl = returnUrl; return(View(details)); }
public async Task <ActionResult> Create(CreateModel model, string returnUrl) { if (ModelState.IsValid) { AppUser defUser = await UserManager.FindByNameAsync("RandomUser"); AppUser user = new AppUser { UserName = model.Name, Email = model.Email, AvatarPath = AppDomain.CurrentDomain.BaseDirectory + "Content\\UserProfiles\\defaultAvatar.jpg", AvatarImageMimeType = "image/jpeg" }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Назначить роль по умолчанию result = await UserManager.AddToRoleAsync(user.Id, "Users"); if (result.Succeeded == false) { AddErrorsFromResult(result); } ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvide.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = true }, ident); } else { AddErrorsFromResult(result); } } return(Redirect(returnUrl ?? "/")); }
public async Task <ActionResult> Login(LoginViewModel details, string returnUrl) { if (ModelState.IsValid) { AppUser user = await UserManager.FindAsync(details.Name, details.Password); if (user == null) { ModelState.AddModelError("", "Invalid name or password."); } // else { //If the FindAsync method does return an AppUser object, //then I need to create the cookie that the browser will send in subsequent requests to show they are authenticated. H ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); //I will use Email-type claim to identify users (I did this in Global.asax/Application_Start()), //external authority will give me this claim //but LOCAL AUTHORIRY will not, so I manually create one ident.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", user.Email)); //invalidate any existing authentication cookies and create the new one AuthManager.SignOut(); AuthManager.SignIn( new AuthenticationProperties { //Keep the cookie persistent, //the user doesn't have to authenticate again when starting a new session IsPersistent = false }, ident); // Migrate the user's shopping cart Session["CartId"] = user.UserName; MigrateShoppingCart(user.UserName); } } ViewBag.returnUrl = returnUrl; return(View(details)); }
public async Task <ActionResult> Login(LoginDto mLogin, string returnUrl) { //var result = _userService.Login(mLogin); //ViewBag.SubmitResult = (string.Format("CallBack({0},\"{1}\")", (int)result, result.ToString())); if (ModelState.IsValid) { User user = await UserManager.FindAsync(mLogin.UserName, mLogin.Password); if (user == null) { ModelState.AddModelError("", "Invalid name or password"); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, ident); if (string.IsNullOrEmpty(returnUrl)) { return(RedirectToAction("Index", "Home", new { area = "Front" })); } else { return(Redirect(returnUrl)); } } } ViewBag.returnUrl = returnUrl; return(View(mLogin)); }