Пример #1
0
        private static string[] GetRolesAndSetCookieInternal()
        {
            string[] currentUserRoles = new string[0];
            String   hostName         = WebUtils.GetHostName();

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

            if (siteSettings != null)
            {
                string roleCookieName = SiteUtils.GetRoleCookieName(siteSettings);
                currentUserRoles = SiteUser.GetRoles(siteSettings, HttpContext.Current.User.Identity.Name);
                string roleStr = "";
                foreach (string role in currentUserRoles)
                {
                    roleStr += role;
                    roleStr += ";";
                }

                if (WebConfigSettings.PreEncryptRolesForCookie)
                {
                    roleStr = SiteUtils.Encrypt(roleStr);
                }

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,                                      // version
                    HttpContext.Current.User.Identity.Name, // user name
                    DateTime.Now,                           // issue time
                    DateTime.Now.AddHours(1),               // expires every hour
                    false,                                  // don't persist cookie
                    roleStr                                 // roles
                    );

                string cookieStr = FormsAuthentication.Encrypt(ticket);

                HttpCookie roleCookie = new HttpCookie(roleCookieName, cookieStr);
                //roleCookie.Expires = DateTime.Now.AddMinutes(20);
                roleCookie.HttpOnly = true;
                roleCookie.Path     = "/";
                if ((SiteUtils.SslIsAvailable()) && WebConfigSettings.RequireSslForRoleCookie)
                {
                    roleCookie.Secure = true;
                }
                HttpContext.Current.Response.Cookies.Add(roleCookie);
            }

            return(currentUserRoles);
        }