public ActionResult Login(string username, string password, bool? rememberMe) { // Basic parameter validation List<string> errors = new List<string>(); if (String.IsNullOrEmpty(username)) { this.ModelState.AddModelError("username", "You must specify a username."); } if (String.IsNullOrEmpty(password)) { this.ModelState.AddModelError("password", "You must specify a password."); } if (errors.Count == 0) { // Attempt to login bool loginSuccessful = Provider.ValidateUser(username, password); if (loginSuccessful) { MembershipUser user = Provider.GetUser(username, false); string userId = ((Guid)user.ProviderUserKey).ToString(); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes(60), false, userId ); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); this.Response.Cookies.Add(authCookie); //Set the user for the remains of the request... IListenToUser userPrincipal = new ListenToUser(user,UserManager); //this.User = user; this.HttpContext.User = userPrincipal; return ReturnToUrl(); } else { this.ModelState.AddModelError("username", "The username or password provided is incorrect."); } } // If we got this far, something failed, redisplay form ViewData["username"] = username; return View(); }
private bool Login(string username, string password) { bool loginSuccessful = false; if (username.Trim() != string.Empty && password.Trim() != string.Empty && Provider.ValidateUser(username, password)) { loginSuccessful = true; MembershipUser user = Provider.GetUser(username, false); string userId = ((Guid)user.ProviderUserKey).ToString(); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes(60), false, userId ); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); this.Response.Cookies.Add(authCookie); //Set the user for the remains of the request... IListenToUser userPrincipal = new ListenToUser(user, UserManager); //this.User = user; this.HttpContext.User = userPrincipal; } return loginSuccessful; }
void Application_AuthenticateRequest(Object Source, EventArgs Details) { try { string userLocale = ListenTo.Web.Helpers.LocaleHelpers.GetBestRFC3066Locale(this.Context.Request.UserLanguages); if (userLocale != String.Empty) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(userLocale); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(userLocale); } } catch (Exception ex) { throw ex; } System.Web.HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null) return; FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); Guid id = new Guid(authTicket.UserData); IListenToUser user = new ListenToUser( ListenTo.Web.Helpers.IOCHelper.GetMembership().GetUser(), ListenTo.Web.Helpers.IOCHelper.GetUserManager() ); Context.User = user; }