/// <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>
        /// Called when [begin request].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void OnBeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;

            if (string.IsNullOrEmpty(CookieHelper.GetBasicAuthCookie()) && BasicAuthenticationHelper.RequiresAuthentication(context.Request.Path))
            {
                if (!IsAuthenticated(context))
                {
                    SendAuthChallengeHeader(context);
                }
            }
        }
        /// <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);
                }
            }
        }