Exemple #1
0
        /// <summary>
        /// Generates the authentication cookie.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="username">The username.</param>
        /// <param name="authenticationTimeoutInSeconds">The authentication timeout in seconds.</param>
        /// <param name="persistentAuthenticationTimeoutInSeconds">The persistent authentication timeout in seconds.</param>
        /// <param name="userObject">The user object.</param>
        /// <param name="isPersistent">The is persistent.</param>
        /// <param name="cookieGenerationMode">The cookie generation mode.</param>
        /// <param name="domain">The domain to share the cookie in. Set this to null if you don't want to share the cookie in a domain.</param>
        /// <returns></returns>
        internal static HttpCookie GenerateAuthenticationCookie <T>(string username, int authenticationTimeoutInSeconds, int persistentAuthenticationTimeoutInSeconds
                                                                    , T userObject, bool?isPersistent, CookieGenerationMode cookieGenerationMode, string domain) where T : IXmlSerializable
        {
            HttpCookie cookie = null;

            switch (cookieGenerationMode)
            {
            case CookieGenerationMode.RefreshExisting:
            case CookieGenerationMode.UpdateExisting:
                cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                break;

            case CookieGenerationMode.CreateNew:
                cookie = FormsAuthentication.GetAuthCookie(username, isPersistent ?? false);
                break;
            }

            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

            if (!isPersistent.HasValue)
            {
                isPersistent = ticket.IsPersistent;
            }

            DateTime expires = isPersistent.Value ? DateTime.Now.AddSeconds(persistentAuthenticationTimeoutInSeconds)
                                                  : DateTime.Now.AddSeconds(authenticationTimeoutInSeconds);

            string userData = string.Empty;

            switch (cookieGenerationMode)
            {
            case CookieGenerationMode.RefreshExisting: userData = ticket.UserData; break;

            case CookieGenerationMode.CreateNew:
            case CookieGenerationMode.UpdateExisting: userData = XmlSerializationHelper <T> .ConvertToXml(userObject); break;
            }

            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(
                2 // only microsoft knows what the version flag means. 2 is default, 1 works as well...
                , ticket.Name
                , ticket.IssueDate
                , expires
                , isPersistent.Value
                , userData);

            cookie.Value = FormsAuthentication.Encrypt(newTicket);

            if (isPersistent.Value)
            {
                cookie.Expires = expires;
            }

            if (!string.IsNullOrEmpty(domain))
            {
                cookie.Domain = domain;
            }

            return(cookie);
        }