private string GetEncryptedAuthenticationTicket(UserDto user) { var serializeModel = new UserSerializeModel { Id = user.Id, UserName = user.UserName, BanExpiryDate = user.BanExpiryDate, Roles = user.Roles }; var serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(serializeModel); const int ticketVersion = 1; const bool isPersistent = false; var authenticationTicket = new FormsAuthenticationTicket( ticketVersion, user.UserName, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(15), isPersistent, userData); return(FormsAuthentication.Encrypt(authenticationTicket)); }
protected void Application_PostAuthenticateRequest(object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null) { HttpContext.Current.User = new UserPrincipal(); } else { FormsAuthenticationTicket authTicket; try { authTicket = FormsAuthentication.Decrypt(authCookie.Value); } catch (ArgumentException) { HttpContext.Current.User = new UserPrincipal(); return; } var serializer = new JavaScriptSerializer(); UserSerializeModel serializeModel = serializer.Deserialize <UserSerializeModel>(authTicket.UserData); var newUser = new UserPrincipal( authTicket.Name, serializeModel.Id, serializeModel.BanExpiryDate, serializeModel.Roles); HttpContext.Current.User = newUser; } }
private void AddAuthenticationTicketToCookie(UserDto user) { var serializeModel = new UserSerializeModel { Id = user.Id, UserName = user.UserName, BanExpiryDate = user.BanExpiryDate, Roles = user.Roles }; var serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(serializeModel); const int ticketVersion = 1; const bool isPersistent = false; var authenticationTicket = new FormsAuthenticationTicket( ticketVersion, user.UserName, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(15), isPersistent, userData); string encryptedTicket = FormsAuthentication.Encrypt(authenticationTicket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(cookie); }
public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { var user = SimpleMembershipProvider.GetUser(model.UserName); loginListener.OnLogin.Invoke(user); var userModel = new UserSerializeModel { Id = user.Id, Username = user.UserName }; var userData = JsonConvert.SerializeObject(userModel); var ticket = new FormsAuthenticationTicket(1, userModel.Username, DateTime.Now, DateTime.Now.AddMinutes(60), false, userData); string ecryptedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ecryptedTicket); Response.Cookies.Add(faCookie); Session["user"] = user; if (Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } else { return(RedirectToAction("Index", "Home")); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Login(LoginScreenModel data) { AccountBO bo = new AccountBO(); var usr = bo.getUser(data.email, data.password); if (usr != null) { UserSerializeModel serializeModel = new UserSerializeModel() { DisplayName = usr.displayName, Email = usr.email, roles = usr.roleMappings.Select(m => m.role).ToList(), UserId = usr.userId }; //JavaScriptSerializer serializer = new JavaScriptSerializer(); var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 }; string userData = serializer.Serialize(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, usr.displayName, DateTime.Now, DateTime.Now.AddMinutes(20), data.remember, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); return(RedirectToAction("Index", "Home")); } else { ViewBag.ErrorMessage = "Email หรือ Password ไม่ถูกต้อง"; return(View(data)); } }
public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.LoginName, model.Password)) { if (model.LoginName.Equals("") || model.LoginName == null) { return(RedirectToAction("reguser", "user")); } var user = (CustomMembershipUser)Membership.GetUser(model.LoginName, false); // var user = DbManager.GetUser(Login: model.LoginName); if (user != null) { UserSerializeModel userModel = new UserSerializeModel() { UserId = user.UserId, Nickname = user.Nickname }; string userData = JsonConvert.SerializeObject(userModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket ( 1, model.LoginName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData ); string enTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie cookie = new HttpCookie("Cookie25", enTicket); Response.Cookies.Add(cookie); // return RedirectToAction("reguser", "user"); } return(RedirectToAction("Index", "Home")); } } ModelState.AddModelError("", "Username or Password invalid"); return(View(model)); }
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); JavaScriptSerializer serializer = new JavaScriptSerializer(); UserSerializeModel serializeModel = serializer.Deserialize <UserSerializeModel>(authTicket.UserData); UserModel newUser = new UserModel(authTicket.Name) { DisplayName = serializeModel.DisplayName, Email = serializeModel.Email, roles = serializeModel.roles, UserId = serializeModel.UserId }; HttpContext.Current.User = newUser; } }
private void SetUser(HttpActionContext actionContext) { const string authorizationKey = "Authorization"; var headers = actionContext.Request.Headers; if (headers.Any(p => p.Key == authorizationKey)) { var encryptedTicket = headers.Single(p => p.Key == authorizationKey).Value; FormsAuthenticationTicket authTicket; try { authTicket = FormsAuthentication.Decrypt(encryptedTicket.First()); } catch (ArgumentException) { HttpContext.Current.User = new UserPrincipal(); return; } var serializer = new JavaScriptSerializer(); UserSerializeModel serializeModel = serializer.Deserialize <UserSerializeModel>(authTicket.UserData); var newUser = new UserPrincipal( authTicket.Name, serializeModel.Id, serializeModel.BanExpiryDate, serializeModel.Roles); HttpContext.Current.User = newUser; } else { HttpContext.Current.User = new UserPrincipal(); } }