Beispiel #1
0
        public IdentityResult RefreshUserGroupRoles(long userId)
        {
            var user = _userManager.FindById(userId);

            if (user == null)
            {
                throw new ArgumentNullException("User");
            }
            // Remove user from previous roles:
            var oldUserRoles = _userManager.GetRoles(userId);

            if (oldUserRoles.Count > 0)
            {
                // _userManager.RemoveFromRoles(userId, oldUserRoles.ToArray());
            }

            // Find teh roles this user is entitled to from group membership:
            var newGroupRoles = this.GetUserGroupRoles(userId);

            // Get the damn role names:
            var allRoles      = _roleManager.Roles.ToList();
            var addTheseRoles = allRoles.Where(r => newGroupRoles.Any(gr => gr.RoleId == r.Id));
            var roleNames     = addTheseRoles.Select(n => n.Name).ToArray();

            // Add the user to the proper roles
            //_userManager.AddToRoles(userId, roleNames);

            return(IdentityResult.Success);
        }
Beispiel #2
0
        public IHttpActionResult GetListUser()
        {
            var list       = _userManager.Users.ToList();
            var returnList = new List <UserInfoViewModel>();

            foreach (var item in list)
            {
                var listRole  = _userManager.GetRoles(item.Id);
                var viewModel = new UserInfoViewModel
                {
                    Id          = item.Id,
                    Email       = item.Email,
                    Roles       = listRole,
                    PhoneNumber = item.PhoneNumber,
                    Address     = item.Address,
                    UserName    = item.UserName
                };
                returnList.Add(viewModel);
            }
            return(Json(returnList));
        }
Beispiel #3
0
        public override string[] GetRolesForUser(string username)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return(null);
            }

            var cacheKey = string.Format("{0}_role", username);

            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return((string[])HttpRuntime.Cache[cacheKey]);
            }

            var am    = new MyUserManager();
            var roles = am.GetRoles(username);

            if (roles != null)
            {
                HttpRuntime.Cache.Insert(cacheKey, roles, null,
                                         DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);
            }
            return(roles);
        }