/// <devdoc> /// Raises the CreatingCookieEvent if atleast one handler is assigned. /// </devdoc> private void OnCreatingCookie(CreatingCookieEventArgs e) { EventHandler <CreatingCookieEventArgs> handler = _creatingCookie; if (null != handler) { handler(this, e); } }
private static void AuthenticationService_CreatingCookie(object sender, CreatingCookieEventArgs e) { var ticket = new FormsAuthenticationTicket( 1, e.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), e.IsPersistent, e.CustomCredential, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie (FormsAuthentication.FormsCookieName, encryptedTicket) {Expires = DateTime.Now.AddMinutes(30)}; HttpContext.Current.Response.Cookies.Add(cookie); e.CookieIsSet = true; }
/// <devdoc> /// Validates the user credentials. /// </devdoc> /// <param name="username"></param> /// <param name="password"></param> /// <param name="customCredential"></param> /// <param name="isPersistent"></param> /// <param name="setCookie">If this is true, CreatingCookie event is raised, and cookie is set in HttpResponse</param> /// <returns></returns> private bool LoginInternal(string username, string password, string customCredential, bool isPersistent, bool setCookie) { if (null == username) { throw new ArgumentNullException("username"); } if (null == password) { throw new ArgumentNullException("password"); } AuthenticatingEventArgs authEventArgs = new AuthenticatingEventArgs(username, password, customCredential); try { OnAuthenticating(authEventArgs); if (!authEventArgs.AuthenticationIsComplete) { MembershipValidate(authEventArgs); } if (!authEventArgs.Authenticated) { Logout(); } if (authEventArgs.Authenticated && setCookie) { CreatingCookieEventArgs cookieEventArgs = new CreatingCookieEventArgs(username, password, isPersistent, customCredential); OnCreatingCookie(cookieEventArgs); if (!cookieEventArgs.CookieIsSet) { SetCookie(username, isPersistent); } } } catch (Exception e) { LogException(e); throw; } return(authEventArgs.Authenticated); }
public static void CreationCookie(object sender, System.Web.ApplicationServices.CreatingCookieEventArgs e) { ONyRDataSet.SysUserDataTable users; ONyRDataSet.SysSessionDataTable sessions; ONyRDataSet.SysSessionRow session; SysUserTableAdapter userAdapter = new SysUserTableAdapter(); users = userAdapter.GetDataByUserName(e.UserName); if (users.Count == 0) { throw new ONyRException(ErrorCode.InvalidCredentialsError); } ONyRDataSet.SysUserRow user = users[0]; SysSessionTableAdapter sessionAdapter = new SysSessionTableAdapter(); sessions = sessionAdapter.GetDataByUserID(user.ID); foreach (ONyRDataSet.SysSessionRow row in sessions) { sessionAdapter.Delete1(row.ID); } sessionAdapter.CreateSession(user.ID); sessions = sessionAdapter.GetDataByUserID(user.ID); session = sessions[0]; sessionAdapter.Dispose(); userAdapter.UpdateLoginDate(user.ID); userAdapter.Dispose(); HttpContext.Current.Response.Cookies.Add(new HttpCookie("UserId", user.ID.ToString())); HttpContext.Current.Response.Cookies.Add(new HttpCookie("SessionId", session.ID.ToString())); }
/// <devdoc> /// Raises the CreatingCookieEvent if atleast one handler is assigned. /// </devdoc> private void OnCreatingCookie(CreatingCookieEventArgs e) { EventHandler<CreatingCookieEventArgs> handler = _creatingCookie; if (null != handler) { handler(this, e); } }
/// <devdoc> /// Validates the user credentials. /// </devdoc> /// <param name="username"></param> /// <param name="password"></param> /// <param name="customCredential"></param> /// <param name="isPersistent"></param> /// <param name="setCookie">If this is true, CreatingCookie event is raised, and cookie is set in HttpResponse</param> /// <returns></returns> private bool LoginInternal(string username, string password, string customCredential, bool isPersistent, bool setCookie) { if (null == username) { throw new ArgumentNullException("username"); } if (null == password) { throw new ArgumentNullException("password"); } AuthenticatingEventArgs authEventArgs = new AuthenticatingEventArgs(username, password, customCredential); try { OnAuthenticating(authEventArgs); if (!authEventArgs.AuthenticationIsComplete) { MembershipValidate(authEventArgs); } if (!authEventArgs.Authenticated) { Logout(); } if (authEventArgs.Authenticated && setCookie) { CreatingCookieEventArgs cookieEventArgs = new CreatingCookieEventArgs(username, password, isPersistent, customCredential); OnCreatingCookie(cookieEventArgs); if (!cookieEventArgs.CookieIsSet) { SetCookie(username, isPersistent); } } } catch (Exception e) { LogException(e); throw; } return authEventArgs.Authenticated; }