public ActionResult Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } bool isValidUser = Membership.ValidateUser(model.Username, model.Password); if (isValidUser) { View_User user = null; using (AruKamiEntities dc = new AruKamiEntities()) { dc.Configuration.ProxyCreationEnabled = false; user = dc.View_User.FirstOrDefault(a => a.Username.Equals(model.Username)); } if (user != null) { JavaScriptSerializer js = new JavaScriptSerializer(); string data = js.Serialize(user); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.Username, DateTime.Now, DateTime.Now.AddMinutes(30), model.RememberMe, data); string encToken = FormsAuthentication.Encrypt(ticket); HttpCookie authoCookies = new HttpCookie(FormsAuthentication.FormsCookieName, encToken); Response.Cookies.Add(authoCookies); return(RedirectToAction("Index", "Home")); } } ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); }
public ProfileViewModel loadInfo(decimal idCard) { ProfileViewModel model = new ProfileViewModel(); View_User user = db.View_User.FirstOrDefault(m => m.IdCard == idCard); model.IdCard = idCard; model.Username = user.Username; model.FirstName = user.FirstName; model.MiddleName = user.MiddleName; model.LastName = user.LastName; model.SecondLastName = user.SecondLastName; return(model); }
protected void Application_PostAuthenticateRequest() { HttpCookie authoCookies = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authoCookies != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authoCookies.Value); JavaScriptSerializer js = new JavaScriptSerializer(); View_User user = js.Deserialize <View_User>(ticket.UserData); MyIdentity myIdentity = new MyIdentity(user); MyIdentity.MyPrincipal myPrincipal = new MyIdentity.MyPrincipal(myIdentity); HttpContext.Current.User = myPrincipal; } }
public ActionResult UserProfile() { bool error = false; var identity = (System.Web.HttpContext.Current.User as MyIdentity.MyPrincipal).Identity as MyIdentity; View_User loggedUser = db.View_User.FirstOrDefault(m => m.IdCard == identity.User.IdCard); ProfileEditViewModel model = new ProfileEditViewModel(); model.profileData = new ProfileViewModel(); model.profileData.IdCard = loggedUser.IdCard; model.profileData.FirstName = loggedUser.FirstName; model.profileData.LastName = loggedUser.LastName; model.profileData.Username = loggedUser.Username; model.profileData.MiddleName = loggedUser.MiddleName; model.profileData.SecondLastName = loggedUser.SecondLastName; return(View(model)); }
public MyIdentity(View_User user) { Identity = new GenericIdentity(user.Username); User = user; }