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(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(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(LoginModel details, string returnUrl) { if (ModelState.IsValid) { var 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); var roleIds = user.Roles.Select(p => p.RoleId).ToList(); var claims = ClaimInfoManager.GetClaimInfos(roleIds); ident.AddClaims(LocationClaimsProvider.GetClaims(claims)); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect(returnUrl)); } } ViewBag.returnUrl = 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> 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)); }
[ValidateAntiForgeryToken]//该属性与视图中的Html.AntiForgeryToken辅助器方法联合工作,防止Cross-Site Request Forgery(CSRF,跨网站请求伪造) public async Task <ActionResult> Login(LoginModel deteils, string returnUrl) { //模型验证通过 if (ModelState.IsValid) { //根据用户名与密码获取操作对象 AppUser user = await UserManager.FindAsync(deteils.Name, deteils.Password); //对象为空 if (user == null) { ModelState.AddModelError("", "Invalid name or password."); } //对象存在 else { //创建Cookie,浏览器会在后继的请求中发送这个Cookie,表明他们是已认证的 //创建一个标识该用户的ClaimsIdentity对象,该实例是调用用户管理器的CreateIdentityAsync方法创建的 //传递一个用户对象和DefaultAuthenticationTypes枚举中的一个值 ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); //签出用户,这通常意味着使标识已认证用户的Cookie失效 AuthManager.SignOut(); //签入用户,这意味着要创建用来标识已认证请求的Cookie AuthManager.SignIn( new AuthenticationProperties //该类用来配置认证过程以及ClaimsIdentity对象 { IsPersistent = false //使认证Cookie在浏览器中是持久化的,意即用户在开始新会话时不必再次认证 }, ident ); return(Redirect(returnUrl)); } } ViewBag.returnUrl = returnUrl; return(View(deteils)); }
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)); }