diff options
Diffstat (limited to 'src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs')
-rw-r--r-- | src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs | 95 |
1 files changed, 73 insertions, 22 deletions
diff --git a/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs b/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs index 1c8d210..496e420 100644 --- a/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs +++ b/src/DotNetOpenAuth.AspNet/OpenAuthAuthenticationTicketHelper.cs @@ -15,17 +15,26 @@ namespace DotNetOpenAuth.AspNet { /// Helper methods for setting and retrieving a custom forms authentication ticket for delegation protocols. /// </summary> internal static class OpenAuthAuthenticationTicketHelper { + #region Constants and Fields + + /// <summary> + /// The open auth cookie token. + /// </summary> private const string OpenAuthCookieToken = "OpenAuth"; - public static void SetAuthenticationTicket(HttpContextBase context, string userName, bool createPersistentCookie) { - if (!context.Request.IsSecureConnection && FormsAuthentication.RequireSSL) { - throw new HttpException(WebResources.ConnectionNotSecure); - } + #endregion - HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie); - context.Response.Cookies.Add(cookie); - } + #region Public Methods and Operators + /// <summary> + /// The is valid authentication ticket. + /// </summary> + /// <param name="context"> + /// The context. + /// </param> + /// <returns> + /// The is valid authentication ticket. + /// </returns> public static bool IsValidAuthenticationTicket(HttpContextBase context) { HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) { @@ -33,7 +42,7 @@ namespace DotNetOpenAuth.AspNet { } string encryptedCookieData = cookie.Value; - if (String.IsNullOrEmpty(encryptedCookieData)) { + if (string.IsNullOrEmpty(encryptedCookieData)) { return false; } @@ -45,28 +54,68 @@ namespace DotNetOpenAuth.AspNet { } } + /// <summary> + /// The set authentication ticket. + /// </summary> + /// <param name="context"> + /// The context. + /// </param> + /// <param name="userName"> + /// The user name. + /// </param> + /// <param name="createPersistentCookie"> + /// The create persistent cookie. + /// </param> + /// <exception cref="HttpException"> + /// </exception> + public static void SetAuthenticationTicket(HttpContextBase context, string userName, bool createPersistentCookie) { + if (!context.Request.IsSecureConnection && FormsAuthentication.RequireSSL) { + throw new HttpException(WebResources.ConnectionNotSecure); + } + + HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie); + context.Response.Cookies.Add(cookie); + } + + #endregion + + #region Methods + + /// <summary> + /// The get auth cookie. + /// </summary> + /// <param name="userName"> + /// The user name. + /// </param> + /// <param name="createPersistentCookie"> + /// The create persistent cookie. + /// </param> + /// <returns> + /// </returns> + /// <exception cref="HttpException"> + /// </exception> private static HttpCookie GetAuthCookie(string userName, bool createPersistentCookie) { - Debug.Assert(!String.IsNullOrEmpty(userName)); + Debug.Assert(!string.IsNullOrEmpty(userName)); var ticket = new FormsAuthenticationTicket( - /* version */ 2, - userName, - DateTime.Now, - DateTime.Now.Add(FormsAuthentication.Timeout), - createPersistentCookie, - OpenAuthCookieToken, - FormsAuthentication.FormsCookiePath); + /* version */ + 2, + userName, + DateTime.Now, + DateTime.Now.Add(FormsAuthentication.Timeout), + createPersistentCookie, + OpenAuthCookieToken, + FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); if (encryptedTicket == null || encryptedTicket.Length < 1) { throw new HttpException(WebResources.FailedToEncryptTicket); } - var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { - HttpOnly = true, - Path = FormsAuthentication.FormsCookiePath, - Secure = FormsAuthentication.RequireSSL - }; + var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) + { + HttpOnly = true, Path = FormsAuthentication.FormsCookiePath, Secure = FormsAuthentication.RequireSSL + }; if (FormsAuthentication.CookieDomain != null) { cookie.Domain = FormsAuthentication.CookieDomain; @@ -78,5 +127,7 @@ namespace DotNetOpenAuth.AspNet { return cookie; } + + #endregion } -}
\ No newline at end of file +} |