public ActionResult Login(LoginModel model, string returnUrl) { model.Password = "******"; model.RememberMe = true; if (WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { //string roles = String.Join(",", Roles.GetRolesForUser(model.UserName)); //FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( // 1, // model.UserName, //user id // DateTime.Now, // DateTime.Now.AddMinutes(600), // expiry // false, //do not remember // roles, // "/"); //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, // FormsAuthentication.Encrypt(authTicket)); //cookie.Expires = DateTime.Now.AddMinutes(600); //Response.Cookies.Add(cookie); StudentProfile student = db.StudentProfiles.Find(model.UserName); if (student != null) { Session["StudentName"] = student.name; } return RedirectToLocal(returnUrl); } else { // Attempt to register the user try { StudentProfile student = db.StudentProfiles.Find(model.UserName); if (student != null) { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); if (student.academic_career == "UGRD") { // Add Role to User Roles.AddUserToRole(model.UserName, "StudentUGRD"); } if (student.academic_career == "RPG") { // Add Role to User Roles.AddUserToRole(model.UserName, "StudentRPGTPG"); } if (student.academic_career == "TPG") { // Add Role to User Roles.AddUserToRole(model.UserName, "StudentRPGTPG"); } if (student.academic_career == "NUGD") { // Add Role to User Roles.AddUserToRole(model.UserName, "StudentNUGD"); } WebSecurity.Login(model.UserName, model.Password); Session["StudentName"] = student.name; return RedirectToAction("Index", "Home"); } } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); Session.Remove("StudentName"); return View(model); }
public ActionResult Login(LoginModel model, string returnUrl) { //if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) //{ // return RedirectToLocal(returnUrl); //} //else //{ // // Attempt to register the user // try // { // int studentId; // if (int.TryParse(model.UserName, out studentId)) // { // StudentProfile student = db.StudentProfiles.Find(studentId); // if (student != null) // { // WebSecurity.CreateUserAndAccount(model.UserName, model.Password); // if (student.academic_career == "UGRD") // { // // Add Role to User // Roles.AddUserToRole(model.UserName, "StudentUGRD"); // } // if (student.academic_career == "RPG") // { // // Add Role to User // Roles.AddUserToRole(model.UserName, "StudentRPGTPG"); // } // if (student.academic_career == "TPG") // { // // Add Role to User // Roles.AddUserToRole(model.UserName, "StudentRPGTPG"); // } // if (student.academic_career == "NUGD") // { // // Add Role to User // Roles.AddUserToRole(model.UserName, "StudentNUGD"); // } // WebSecurity.Login(model.UserName, model.Password); // return RedirectToAction("Index", "Home"); // } // } // } // catch (MembershipCreateUserException e) // { // ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); // } //} //// If we got this far, something failed, redisplay form //ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(model); }