/// <summary> /// Determines whether the specified context is authenticated. /// </summary> /// <param name="context">The context.</param> /// <returns> /// <c>true</c> if the specified context is authenticated; otherwise, <c>false</c>. /// </returns> private bool IsAuthenticated(HttpApplication context) { string authHeader = context.Request.Headers["Authorization"]; if (!string.IsNullOrEmpty(authHeader)) { if (authHeader.StartsWith("basic ", StringComparison.InvariantCultureIgnoreCase)) { string userNameAndPassword = Encoding.Default.GetString( Convert.FromBase64String(authHeader.Substring(6))); string[] parts = userNameAndPassword.Split(':'); BasicUser bu = new BasicUser(); bu.UserName = parts[0]; bu.Password = parts[1]; if (BasicAuthenticationHelper.Authenticate(bu.UserName, bu.Password)) { CookieHelper.SetBasicAuthCookie(bu); return(true); } else { if (!string.IsNullOrEmpty(CookieHelper.GetBasicAuthCookie())) { CookieHelper.RemoveBasicAuthCookie(); } return(false); } } } return(false); }
/// <summary> /// Determines whether the specified context is authenticated. /// </summary> /// <param name="context">The context.</param> /// <returns> /// <c>true</c> if the specified context is authenticated; otherwise, <c>false</c>. /// </returns> private bool IsAuthenticated(HttpApplication context) { string authHeader = context.Request.Headers["Authorization"]; if(!string.IsNullOrEmpty(authHeader)) { if(authHeader.StartsWith("basic ", StringComparison.InvariantCultureIgnoreCase)) { string userNameAndPassword = Encoding.Default.GetString( Convert.FromBase64String(authHeader.Substring(6))); string[] parts = userNameAndPassword.Split(':'); BasicUser bu = new BasicUser(); bu.UserName = parts[0]; bu.Password = parts[1]; if(BasicAuthenticationHelper.Authenticate(bu.UserName, bu.Password)) { CookieHelper.SetBasicAuthCookie(bu); return true; } else { if(!string.IsNullOrEmpty(CookieHelper.GetBasicAuthCookie())) { CookieHelper.RemoveBasicAuthCookie(); } return false; } } } return false; }
/// <summary> /// Sets the basic auth cookie. /// </summary> /// <param name="user">The user.</param> internal static void SetBasicAuthCookie(BasicUser user) { if (user != null) { XmlSerializer s = new XmlSerializer(typeof(BasicUser)); StringWriter sw = new StringWriter(); s.Serialize(sw, user); string value = sw.ToString(); SetBasicAuthCookie(value); } }
/// <summary> /// Called when [authorize]. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void OnAuthorize(object sender, EventArgs e) { HttpApplication context = sender as HttpApplication; if (BasicAuthenticationHelper.RequiresAuthentication(context.Request.Path)) { BasicUser bu = CookieHelper.GetBasicUser(); if (bu == null || !(BasicAuthenticationHelper.Authenticate(bu.UserName, bu.Password))) { SendNotAuthorizedHeader(context); } } }
/// <summary> /// Sets the basic auth cookie. /// </summary> /// <param name="user">The user.</param> /// <param name="expires">The expires.</param> internal static void SetBasicAuthCookie(BasicUser user, int expires) { SetBasicAuthCookie(user, BasicAuthenticationHelper.CookieExpires); }
/// <summary> /// Sets the basic auth cookie. /// </summary> /// <param name="user">The user.</param> internal static void SetBasicAuthCookie(BasicUser user) { if(user != null) { XmlSerializer s = new XmlSerializer(typeof(BasicUser)); StringWriter sw = new StringWriter(); s.Serialize(sw, user); string value = sw.ToString(); SetBasicAuthCookie(value); } }