Esempio n. 1
0
        // public is for test purposes.
        public async Task <DbUser> CreateUserAsync(ClaimsUser claimsUser)
        {
            _emailDomainValidatorService.Validate(claimsUser.Email);

            var user = new DbUser
            {
                UserName       = claimsUser.Email,
                Email          = claimsUser.Email,
                FirstName      = claimsUser.FirstName,
                LastName       = claimsUser.LastName,
                EmailConfirmed = true
            };

            IdentityResult createUserResult = await _userManager.CreateAsync(user);

            if (!createUserResult.Succeeded)
            {
                throw CreateException($"Cannot create new user '{claimsUser.Email}'\r\n", createUserResult);
            }

            string role = (await ChooseUserRoleAsync()).ToString();

            IdentityResult roleAddingResult = await _userManager.AddToRoleAsync(user, role);

            if (!roleAddingResult.Succeeded)
            {
                throw CreateException($"Cannot add role '{role}' to user '{claimsUser.Email}'\r\n", roleAddingResult);
            }

            var claimsAddingResult = await _userManager.AddClaimsAsync(
                user : user,
                claims : claimsUser.Claims().Concat(user.Claims(role)));

            if (!claimsAddingResult.Succeeded)
            {
                throw CreateException($"Cannot add claims to user '{claimsUser.Email}'\r\n", claimsAddingResult);
            }

            return(user);
        }