Ejemplo n.º 1
0
        public async Task <string> CreateUserAsync(CoreModels.User user)
        {
            Guard.ArgumentIsNotNull(user, nameof(user));

            UserValidator.Validate(user);

            ValdiateIfUserWithSameLoginExists(user);

            IdentityUser identityUser = new IdentityUser
            {
                Id       = Guid.NewGuid().ToString(),
                UserName = user.Login,
                Email    = user.FullName
            };

            foreach (Role role in user.Roles)
            {
                identityUser.Roles.Add(new IdentityUserRole
                {
                    UserId = identityUser.Id,
                    RoleId = role.Id
                });
                identityUser.Claims.Add(new IdentityUserClaim
                {
                    ClaimType  = ClaimTypes.Role,
                    ClaimValue = role.Name,
                    UserId     = identityUser.Id
                });
            }
            identityUser.LockoutEnabled = true;
            identityUser.PasswordHash   = _userManager.PasswordHasher.HashPassword(user.Password);

            IdentityResult result = await _userManager.CreateAsync(identityUser, user.Password);

            return(result.Succeeded ? identityUser.Id : string.Empty);
        }
Ejemplo n.º 2
0
        public async Task <IdentityResult> UpdateUserAsync(CoreModels.User user)
        {
            Guard.ArgumentIsNotNull(user, nameof(user));
            bool securityStampNeedToBeChanged = false;

            UserValidator.Validate(user);

            ValdiateIfUserWithSameLoginExists(user);

            IdentityUser identityUser = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == user.UserId);

            if (identityUser == null)
            {
                return(new IdentityResult("IdentityUser is null"));
            }

            if (identityUser.UserName != user.Login)
            {
                identityUser.UserName        = user.Login;
                securityStampNeedToBeChanged = true;
            }

            identityUser.Email = user.FullName;

            identityUser.Roles.Clear();

            foreach (Role role in user.Roles)
            {
                identityUser.Roles.Add(new IdentityUserRole
                {
                    UserId = user.UserId,
                    RoleId = role.Id
                });

                //identityUser.Claims.Add(new IdentityUserClaim
                //{
                //    Id = maxClaimId++,
                //    ClaimType = ClaimTypes.Role,
                //    ClaimValue = role.Name,
                //    UserId = user.UserId
                //});
            }

            if (!string.IsNullOrEmpty(user.Password))
            {
                identityUser.PasswordHash    = _userManager.PasswordHasher.HashPassword(user.Password);
                securityStampNeedToBeChanged = true;
            }

            IdentityResult result = await _userManager.UpdateAsync(identityUser);

            if (result.Succeeded && securityStampNeedToBeChanged)
            {
                await _userManager.UpdateSecurityStampAsync(identityUser.Id);
            }

            if (result.Succeeded)
            {
                // TODO: refactor. Is needed because UserManager throws an exception "UserId" is required if Id is empty
                await ClearClaimsManuallyAsync(identityUser.Id);

                foreach (Role role in user.Roles)
                {
                    await AddClaimsManuallyAsync(user.UserId, role.Name);
                }
            }

            return(result);
        }