コード例 #1
0
        public async Task <User> RegisterUser(RegisterUserModel model)
        {
            if (!await registerUserPolicy.IsValid(model))
            {
                throw new PolicyViolationException(registerUserPolicy.PolicyViolations);
            }

            model.Password = "******";
            var salt           = encrypter.GenerateSalt();
            var hashedPassword = encrypter.GenerateHash(model.Password, salt);

            var user = dbContext.Users.Add(new User(userInfoProvider.Username)
            {
                CreatedBy                = userInfoProvider.Username,
                LastModifiedBy           = userInfoProvider.Username,
                Firstname                = model.Firstname,
                Lastname                 = model.Lastname,
                Username                 = model.Username,
                ResetPasswordOnNextLogin = true,
                PasswordHash             = model.IsAdUser ? null : hashedPassword,
                Salt = salt,
                IsActiveDirectoryUser = model.IsAdUser,
                Email = model.Email
            });

            CreateRoles(model.Roles, user.Entity);

            await dbContext.SaveChangesAsync();

            return(user.Entity);
        }