public static void SetAuthCookie(ILoginUser user) { Initialize(); if (!HttpContext.Current.Request.IsSecureConnection && RequireSSL) { throw new HttpException("Connection_not_secure_creating_secure_cookie"); } bool fromCookie = false; ILibAuthenticationTicket ticket = LibAuthentication.ExtractTicketFromCookie(ref fromCookie); if (ticket == null) { ticket = LibAuthentication.CreateTicket(user); } HttpCookie cookie = LibAuthentication.PrepareCookie(ticket, false); if (fromCookie) { HttpContext.Current.Response.Cookies.Remove(cookie.Name); HttpContext.Current.Response.Cookies.Add(cookie); } else { HttpContext.Current.Response.Cookies.Add(cookie); } }
protected virtual void Authenticate(LibAuthenticationEventArgs e) { if (this.OnAuthenticate != null) { this.OnAuthenticate(this, e); } if (e.Context.User == null) { if (e.User != null) { e.Context.User = e.User; } else { bool fromCookie = true; ILibAuthenticationTicket ticket = LibAuthentication.ExtractTicketFromCookie(ref fromCookie); if (ticket == null) { LibAuthentication.RedirectLogin(e.Context); return; } else { LibAuthentication.PrepareTicket(ticket); e.Context.User = new LibPrincipal(new LibIdentity(ticket)); HttpCookie cookie = LibAuthentication.PrepareCookie(ticket, fromCookie); e.Context.Response.Cookies.Remove(cookie.Name); e.Context.Response.Cookies.Add(cookie); } } } }
public static HttpCookie PrepareCookie(ILibAuthenticationTicket ticket, bool fromCookie) { HttpCookie cookie = null; if (fromCookie && !LibAuthentication.CookiePath.Equals("/")) { cookie = HttpContext.Current.Request.Cookies[LibAuthentication.CookieName]; if (cookie != null) { cookie.Path = LibAuthentication.CookiePath; } } if (cookie == null) { cookie = new HttpCookie(LibAuthentication.CookieName); cookie.Path = LibAuthentication.CookiePath; } if (ticket.IsPersistent) { cookie.Expires = ticket.LoginTimeout; } cookie.Value = LibAuthentication.Encrypt(ticket); cookie.Secure = LibAuthentication.RequireSSL; cookie.HttpOnly = true; if (LibAuthentication.CookieDomain != null) { cookie.Domain = LibAuthentication.CookieDomain; } return(cookie); }
public LibIdentity(ILibAuthenticationTicket ticket) { if (ticket == null) { throw new ArgumentNullException("ticket"); } this.ticket = ticket; }
public static ILibAuthenticationTicket PrepareTicket(ILibAuthenticationTicket ticket) { if (ticket != null && !ticket.Expired) { if (LibAuthentication.SlidingExpiration) { LibAuthentication.RenewTicket(ticket); } } return(ticket); }
public LibAuthenticationTicket(ILibAuthenticationTicket ticket) { this.name = ticket.Name; this.loginSessionID = ticket.LoginSessionID; this.loginIP = ticket.LoginIP; this.authenticateServer = ticket.AuthenticateServer; this.loginTime = ticket.LoginTime; this.LoginTimeout = LibAuthentication.SlidingExpiration ? DateTime.Now.AddMinutes(LibAuthentication.CookieTimeout) : ticket.LoginTimeout; this.isPersistent = ticket.IsPersistent; this.loginUser = ticket.LoginUser; }
/// <summary> /// 加密票据数据 /// </summary> /// <param name="ticket">票据信息</param> /// <param name="oParam">加密参数</param> /// <returns>加密后字节</returns> public byte[] EncryptTicket(ILibAuthenticationTicket ticket, object oParam) { string strKeyInfo = (string)oParam; CspParameters cspParams = new CspParameters(); cspParams.Flags = CspProviderFlags.UseMachineKeyStore; cspParams.KeyContainerName = "TicketContainer"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams); rsa.FromXmlString(strKeyInfo); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(ticket.Serialize()); return(RSAEncryptData(dataToEncrypt, rsa, false)); }
public static ILibAuthenticationTicket ExtractTicketFromCookie(ref bool fromCookie) { ILibAuthenticationTicket ticket = null; if (fromCookie) { HttpCookie cookie = HttpContext.Current.Request.Cookies[LibAuthentication.CookieName]; if (cookie != null) { ticket = LibAuthentication.Decrypt(cookie.Value); fromCookie = true; } else { fromCookie = false; } } return(ticket); }
public static string Encrypt(ILibAuthenticationTicket ticket) { return(ticket.Serialize()); }
public static void RenewTicket(ILibAuthenticationTicket ticket) { ticket = new LibAuthenticationTicket(ticket); }
public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) { ILibAuthenticationTicket obj = CreateObject(dictionary, type, serializer); return(obj); }