Esempio n. 1
0
        public async Task <IdentityResult> CreateWithClaimsAsync(RoleDto role)
        {
            Role roleToCreate = new Role()
            {
                Id = 0, Name = role.Name
            };
            IdentityResult result = await CreateAsync(roleToCreate);

            if (result == IdentityResult.Success)
            {
                Role createdRole = await FindRoleByNameAsync(roleToCreate.Name);

                List <Claim> claimsToAdd = new List <Claim>();
                foreach (string claimToInsert in role.Claims)
                {
                    foreach (Claim existingClaim in RoleClaims.GetAllClaims())
                    {
                        if (existingClaim.Value.Equals(claimToInsert))
                        {
                            claimsToAdd.Add(existingClaim);
                        }
                    }
                }
                foreach (Claim newClaim in claimsToAdd)
                {
                    await AddClaimAsync(createdRole, newClaim);
                }
            }
            return(result);
        }
Esempio n. 2
0
        public async Task <IActionResult> GetAllClaims(long id)
        {
            List <string> list = new List <string>();

            foreach (Claim claim in await Task.FromResult(RoleClaims.GetAllClaims()))
            {
                list.Add(claim.Value);
            }
            return(Ok(list));
        }
Esempio n. 3
0
        public async Task <IActionResult> Login([FromBody] CredentialsDto credentials)
        {
            var user = await _userService.FindByNameAsync(credentials.UserNameOrEmail);

            if (user == null)
            {
                user = await _userService.FindByEmailAsync(credentials.UserNameOrEmail);

                if (user == null)
                {
                    return(BadRequest("Login fehlgeschlagen!"));
                }
            }
            user.LastLoginDate = DateTime.Now;
            await _userService.UpdateUserAsync(user);

            await userLoginService.CreateAsync(new UserLogin()
            {
                DateTimeOfLastLogin = DateTime.Now, UserId = user.Id
            });

            if (user.UserLockEnabled)
            {
                return(BadRequest("Benutzer ist gesperrt! Bitte den Administrator kontaktieren"));
            }

            var signInAsync = await _signInService.CheckPasswordSignInAsync(user, credentials.Password);

            if (signInAsync.Succeeded)
            {
                var userDto = _mapper.Map <UserDto>(user);
                var roles   = await _userService.GetRolesAsync(user);

                List <Claim> claims;
                if (!user.IsSuperAdmin)
                {
                    claims = await GetAllClaimsOfUser(roles);
                }
                else
                {
                    claims = RoleClaims.GetAllClaims();
                }
                userDto.AccessToken = _signInService.CreateToken(user, roles, claims);
                return(Ok(userDto));
            }

            return(BadRequest("Login fehlgeschlagen!"));
        }
Esempio n. 4
0
        public async Task <IdentityResult> UpdateRoleWithClaimsAsync(RoleDto role)
        {
            Role roleToUpdate = await FindRoleByIdAsync(role.Id);

            if (roleToUpdate == null)
            {
                return(IdentityResult.Failed(new IdentityError()
                {
                    Code = "301", Description = "Role not found!"
                }));
            }
            if (!roleToUpdate.Name.Equals(role.Name) && role.Id > 2)
            {
                if (!await CheckIfRoleNameAlreadyExistsAsync(role.Name))
                {
                    roleToUpdate.Name = role.Name;
                    await UpdateAsync(roleToUpdate);
                }
            }

            List <Claim>  claimsToDelete = new List <Claim>();
            IList <Claim> existingClaims = await GetClaimsAsync(roleToUpdate);

            foreach (Claim existingClaim in existingClaims)
            {
                if (role.Claims.FirstOrDefault(x => x.Equals(existingClaim.Value)) == null)
                {
                    claimsToDelete.Add(existingClaim);
                }
            }
            foreach (Claim claimToDelete in claimsToDelete)
            {
                await RemoveClaimAsync(roleToUpdate, claimToDelete);
            }
            List <Claim> claimsToAdd = new List <Claim>();
            bool         error       = false;

            foreach (string claimToAdd in role.Claims)
            {
                if (existingClaims.FirstOrDefault(x => x.Value.Equals(claimToAdd)) == null)
                {
                    Claim existingClaimToAdd = RoleClaims.GetAllClaims().FirstOrDefault(x => x.Value.Equals(claimToAdd));
                    if (existingClaimToAdd != null)
                    {
                        claimsToAdd.Add(existingClaimToAdd);
                    }
                    else
                    {
                        error = true;
                    }
                }
            }
            foreach (Claim claimToAdd in claimsToAdd)
            {
                await AddClaimAsync(roleToUpdate, claimToAdd);
            }
            if (!error)
            {
                return(IdentityResult.Success);
            }
            else
            {
                return(IdentityResult.Failed(new IdentityError()
                {
                    Code = "201", Description = "Claim not found!"
                }));
            }
        }