コード例 #1
0
        public async Task <string> RegisterAsync(RegisterUserCommand command)
        {
            var user = new ApplicationUser
            {
                UserName       = command.Email,
                Email          = command.Email,
                EmailConfirmed = true
            };
            var userWithSameEmail = await _userManagerWrapper.FindByEmailAsync(command.Email);

            if (userWithSameEmail != null)
            {
                return($"Email {user.Email} is already registered.");
            }

            var result = await _userManagerWrapper.CreateAsync(user, command.Password);

            if (!result.Succeeded)
            {
                return(string.Join(" ", result.Errors.Select(x => x.Description)));
            }

            await _userManagerWrapper.AddToRoleAsync(user, Authorization.DEFAULT_ROLE.ToString());

            return($"User Registered with username {user.UserName}");
        }
コード例 #2
0
        public async Task <IdentityResult> AddUser(UserForRegister user)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                var userEntity = mapper.Map <User>(user);
                var result     = await userManager.CreateAsync(userEntity, user.Password);

                if (result.Succeeded)
                {
                    result = await userManager.AddToRoleAsync(userEntity, Roles.Customer);
                }

                if (result.Succeeded)
                {
                    transaction.Commit();
                }

                return(result);
            }
        }