Beispiel #1
0
        /// <summary>
        /// Creates Principal and Identity based on the user name and roles from the 
        /// asp.net authentication cookie;
        /// </summary>
        /// <returns>The current principal</returns>
        public static IPrincipal GetPrincipalFromCookie(IIdentity identity)
        {
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];

            if (authCookie == null)
            {
                // There is no authentication cookie.
                return SetEmptyPrincipal();
            }

            FormsAuthenticationTicket authTicket = null;
            try
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            }
            catch
            {
                // error occured. Let user authenticate again
                return SetEmptyPrincipal();
            }

            if (authTicket == null)
            {
                // Cookie failed to decrypt.
                return SetEmptyPrincipal();
            }

            // Whenever we load cookie we always load the Roles instead of loading it from Userdata
            string[] roles = Roles.GetRolesForUser(identity.Name);

            IPrincipal principal = new VKeCRMPrincipal(identity, roles);
            HttpContext.Current.User = principal;
            return principal;
        }
Beispiel #2
0
 /// <summary>
 /// Sets the HttpContext.Current.User to an empty principal
 /// </summary>
 /// <returns>The empty principal</returns>
 private static IPrincipal SetEmptyPrincipal()
 {
     IIdentity identity = new VKeCRMIdentity();
     IPrincipal principal = new VKeCRMPrincipal(identity, new string[0]);
     HttpContext.Current.User = principal;
     return principal;
 }