Exemple #1
0
        // Delete User
        public void DeleteUser(string id)
        {
            var user = UserManager.FindById(id);

            // Initialize UserAccount
            var userIdentity    = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie) as ClaimsIdentity;
            var claimsPrincipal = new ClaimsPrincipal(userIdentity);

            var userAccount = new RequestUser(claimsPrincipal);

            // Get all claims
            var claims = claimsPrincipal.Claims;
            // Set a flag for successful remove
            var check = true;

            // Remove all claims from user
            foreach (var claim in claims)
            {
                var r = UserManager.RemoveClaimAsync(user.Id, new Claim(claim.Type, claim.Value)).Result;
                if (!r.Succeeded)
                {
                    check = false;
                }
            }

            // Finally remove the user
            if (check)
            {
                var result = UserManager.DeleteAsync(user).Result;
            }
        }
Exemple #2
0
        // Get User by Id
        public ApplicationUserDetail GetUserById(string id)
        {
            // Fetch the User by Id
            var user = UserManager.FindById(id);

            if (user == null)
            {
                return(null);
            }

            // Initialize UserAccount
            var userIdentity    = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie) as ClaimsIdentity;
            var claimsPrincipal = new ClaimsPrincipal(userIdentity);

            var userAccount = new RequestUser(claimsPrincipal);

            // Map user details
            var details = mapper.Map <ApplicationUserDetail>(userAccount);

            details.UserName = user.UserName;
            details.Email    = user.UserName;
            details.Roles    = userAccount.RoleClaims;

            return(details);
        }
Exemple #3
0
        // Edit User Claims - For Now Only Roles
        public ApplicationUserDetail ApplicationUserEdit(ApplicationUserEdit newItem)
        {
            var result = new IdentityResult();

            // Attempt to fetch the object
            var o = UserManager.FindById(newItem.Id);

            if (o == null)
            {
                return(null);
            }

            var userIdentity    = UserManager.CreateIdentity(o, DefaultAuthenticationTypes.ApplicationCookie) as ClaimsIdentity;
            var claimsPrincipal = new ClaimsPrincipal(userIdentity);
            var userAccount     = new RequestUser(claimsPrincipal);

            // Remove all roles
            foreach (var role in userAccount.RoleClaims)
            {
                result = UserManager.RemoveClaimAsync(o.Id, new Claim(ClaimTypes.Role, role)).Result;
            }

            // If successful removal, Add Roles
            if (result.Succeeded)
            {
                foreach (var newRole in newItem.Roles)
                {
                    result = UserManager.AddClaimAsync(o.Id, new Claim(ClaimTypes.Role, newRole)).Result;
                }
                if (result.Succeeded)
                {
                    return(mapper.Map <ApplicationUserDetail>(newItem));
                }
            }
            return(null);
        }