コード例 #1
0
        public string[] GetRolesForCurrentUser()
        {
            try {
                ApplicationServiceHelper.EnsureRoleServiceEnabled();
                EnsureProviderEnabled();

                IPrincipal   user     = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
                string       username = ApplicationServiceHelper.GetUserName(user);
                RoleProvider provider = GetRoleProvider(user);

                return(provider.GetRolesForUser(username));
            }
            catch (Exception e) {
                LogException(e);
                throw;
            }
        }
コード例 #2
0
        public bool IsCurrentUserInRole(string role)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            try {
                ApplicationServiceHelper.EnsureRoleServiceEnabled();
                EnsureProviderEnabled();

                IPrincipal   user     = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
                string       username = ApplicationServiceHelper.GetUserName(user);
                RoleProvider provider = GetRoleProvider(user);

                return(provider.IsUserInRole(username, role));
            }
            catch (Exception e) {
                LogException(e);
                throw;
            }
        }
コード例 #3
0
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        private static ProfileBase GetProfileForCurrentUser(bool authenticatedUserOnly)
        {
            HttpContext context         = HttpContext.Current;
            IPrincipal  user            = ApplicationServiceHelper.GetCurrentUser(context);
            string      name            = null;
            bool        isAuthenticated = false;

            if (user == null || user.Identity == null || string.IsNullOrEmpty(user.Identity.Name))   // anonymous user?
            {
                isAuthenticated = false;

                if (!authenticatedUserOnly && context != null && !string.IsNullOrEmpty(context.Request.AnonymousID))   // Use Anonymous ID?
                {
                    name = context.Request.AnonymousID;
                }
            }
            else
            {
                name            = user.Identity.Name;
                isAuthenticated = user.Identity.IsAuthenticated;
            }

            if (!isAuthenticated && (authenticatedUserOnly || string.IsNullOrEmpty(name)))
            {
                if (context != null)
                {
                    throw new HttpException(AtlasWeb.UserIsNotAuthenticated);
                }
                else
                {
                    throw new Exception(AtlasWeb.UserIsNotAuthenticated);
                }
            }

            return(ProfileBase.Create(name, isAuthenticated));
        }