public static bool SetAuthenticationCookie(UserLogin userLogin) { FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, // ticket version userLogin.EmailAddress, // authenticated username DateTime.Now, // issueDate DateTime.Now.AddMinutes(30), // expiryDate true, // true to persist across browser sessions userLogin.UserLoginID.ToString(), // can be used to store additional user data FormsAuthentication.FormsCookiePath); // the path for the cookie // Encrypt the ticket using the machine key string encryptedTicket = FormsAuthentication.Encrypt(ticket); // Add the cookie to the request to save it HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Add(cookie); // Your redirect logic //HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userLogin.EmailAddress, true)); return true; }
public ActionResult Create(UserLogin userlogin, string accessToken) { if (ModelState.IsValid) { bool IsUserAlreadyExists = true; if (!_userLoginManager.IsUserLoginExists(userlogin.EmailAddress)) { userlogin.VerificationCode = UserLoginHelper.GenerateRandomVerificationCode(); if (userlogin.IsFacebookLogin == true) { userlogin.Password = UserLoginHelper.GenerateRandomPassword(); } _userLoginManager.AddUserLogin(userlogin); IsUserAlreadyExists = false; } if (userlogin.IsFacebookLogin == true) { FaceBookConnect.AccessToken = accessToken; userlogin = _userLoginManager.GetUserLogin(userlogin.EmailAddress); IsUserAlreadyExists = false; } if (IsUserAlreadyExists) { throw new ValidationException(Json(new { Message = string.Format(Resources.MessageResources.UserAlreadyRegisteredMessageFormat, userlogin.EmailAddress) })); } object routeValues = new { area = "User" }; var urlToRedirect = Url.Action("Index", "Profile", routeValues); if (userlogin.IsEmailVerified == true) { Utils.SetAuthenticationCookie(userlogin); UserContext.CurrentUserLoginID = userlogin.UserLoginID; } else { routeValues = new { area = "User", id = userlogin.UserLoginID }; urlToRedirect = Url.Action("VerifyEmail", "Login", routeValues); } return Json(new { redirectToUrl = urlToRedirect, Message = "Success" }); } else { var ModelStateError = from e in ModelState where e.Value.Errors.Count > 0 select e.Value.Errors[0].ErrorMessage; throw new GeneralException(Json(new { userlogin = userlogin, Message = ModelStateError })); } }
public ActionResult ValidateUserLogin(UserLogin userLogin) { var userlogin = _userLoginManager.ValidateUserLogin(userLogin.EmailAddress, userLogin.Password); object routeValues = new { area = "User" }; var urlToRedirect = Url.Action("", "Profile", routeValues); if (userlogin == null) { throw new ValidationException(Json(new { Message = Resources.MessageResources.UserLoginValidationFailedMessage })); } if (!(userlogin.IsActive == true)) { throw new ValidationException(Json(new { Message = Resources.MessageResources.UserAccountInactiveMessage })); } if (!(userlogin.IsEmailVerified == true)) { routeValues = new { area = "User", id = userlogin.UserLoginID }; urlToRedirect = Url.Action("VerifyEmail", "Login", routeValues); } else { Utils.SetAuthenticationCookie(userlogin); UserContext.CurrentUserLoginID = userlogin.UserLoginID; } return Json(new { redirectToUrl = urlToRedirect, Message = "Success" }); }
public void AddUserLogin(UserLogin userLogin) { _db.UserLogins.Add(userLogin); _db.SaveChanges(); }