public static void SetAuthenticationCookie(HttpResponse response, string validUserName) { WebSupport.InitializeContextStorage(HttpContext.Current.Request); string authString = EncryptionSupport.EncryptStringToBase64(validUserName); if (response.Cookies[AuthCookieName] != null) { response.Cookies.Remove(AuthCookieName); } HttpCookie cookie = new HttpCookie(AuthCookieName, authString); cookie.HttpOnly = false; HttpContext.Current.Response.Cookies.Add(cookie); }
public static void SetUserFromCookieIfExists(HttpContext context) { var request = HttpContext.Current.Request; var encCookie = request.Cookies[AuthCookieName]; if (encCookie != null) { try { WebSupport.InitializeContextStorage(context.Request); string userName = EncryptionSupport.DecryptStringFromBase64(encCookie.Value); context.User = new GenericPrincipal(new GenericIdentity(userName, "theball"), new string[0]); // Reset cookie time to be again timeout from this request encCookie.Expires = DateTime.Now.AddSeconds(TimeoutSeconds); context.Response.Cookies.Set(encCookie); } catch { ClearAuthenticationCookie(context.Response); } } }