예제 #1
0
        /// <summary>
        /// Validates authentication cookie and refreshes it if cookie is near to expiration time.
        /// </summary>
        /// <returns>User principal name if cookie is valid. Otherwise - null.</returns>
        private static string GetPrincipalNameFromCookie(HttpContextBase httpContext)
        {
            HttpCookie principalCookie = httpContext.Request.Cookies.Get(AuthCookieName);

            if (principalCookie == null || string.IsNullOrEmpty(principalCookie.Value))
            {
                return(null);
            }

            AuthenticationPackage authPackage = AuthenticationPackage.FromXml(principalCookie.Value);

            if (authPackage == null)
            {
                return(null);
            }

            DateTime now = DateTime.UtcNow;

            if (now > authPackage.Expires)
            {
                return(null);
            }
            else if (now >= authPackage.Expires.AddSeconds(-ConfiguraionProvider.AuthenticationCookieRefreshMargin))
            {
                SetAuthenticationCookie(httpContext, authPackage.PrincipalName);
            }

            return(authPackage.PrincipalName);
        }
예제 #2
0
        private static void SetAuthenticationCookie(HttpContextBase httpContext, string principalName)
        {
            AuthenticationPackage package = new AuthenticationPackage()
            {
                PrincipalName = principalName,
                Expires       = DateTime.UtcNow.AddSeconds(ConfiguraionProvider.AuthenticationCookieDuration)
            };

            HttpCookie principalCookie = httpContext.Response.Cookies[AuthCookieName];

            if (principalCookie == null)
            {
                principalCookie       = new HttpCookie(AuthCookieName);
                principalCookie.Value = package.ToXml();
                httpContext.Response.AppendCookie(principalCookie);
            }
            else
            {
                principalCookie.Value = package.ToXml();
                httpContext.Response.SetCookie(principalCookie);
            }
        }