コード例 #1
0
        public async Task <IActionResult> ValidateUsername(string username, string email)
        {
            IdentityResult result = await _userValidator.ValidateAsync(
                (UserManager <User>) _userManager, new User { UserName = username /*, Email = email*/ });

            return(Json(result.Succeeded ? "true" : result.DumpErrors(useHtmlNewLine: true)));
        }
コード例 #2
0
        public async Task <IActionResult> ValidatePassword(string password, string username)
        {
            IdentityResult result = await _passwordValidator.ValidateAsync(
                (UserManager <User>) _userManager, new User { UserName = username }, password);

            return(Json(result.Succeeded ? "true" : result.DumpErrors(useHtmlNewLine: true)));
        }
コード例 #3
0
        public async Task <IActionResult> CreateUser([FromBody] RegisterViewModel model)
        {
            User user = new User
            {
                UserName  = model.UserName,
                Email     = model.Email,
                FirstName = model.FirstName,
                LastName  = model.LastName
            };
            IdentityResult result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                _logger.LogInformation(3, $"{user.UserName} created a new account with password.");

                if (_settings.EnableEmailConfirmation)
                {
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    await _emailSender.SendEmailAsync(
                        email : user.Email,
                        subject : "Please confirm your email",
                        message : code);

                    return(Ok());
                }
            }
            return(BadRequest(error: result.DumpErrors(useHtmlNewLine: false)));
        }
コード例 #4
0
        public async Task <ApiResult <string> > Register([FromBody] UsersViewModel viewModel)
        {
            ModelState.Remove("roleId");
            ModelState.Remove("PersianBirthDate");
            ModelState.Remove("PhoneNumber");
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser
                {
                    EmailConfirmed = true,
                    UserName       = viewModel.UserName,
                    FirstName      = viewModel.FirstName,
                    LastName       = viewModel.LastName,
                    PasswordHash   = viewModel.Password,
                    Email          = viewModel.Email,
                    Gender         = viewModel.Gender ?? viewModel.Gender,
                    Notifications  = new List <Notification>
                    {
                        new Notification
                        {
                            EnterEmail    = true,
                            ExitEmail     = true,
                            EnterTelegram = true,
                            ExitTelegram  = true,
                            EnterSms      = false,
                            ExitSms       = false,
                        }
                    }
                };
                IdentityResult identityResult = await _userManager.CreateAsync(user, viewModel.Password);

                if (identityResult.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, "کاربر عادی");

                    return(Ok(NotificationMessages.RegisterSuccess));
                }

                return(BadRequest(identityResult.DumpErrors()));
            }

            return(BadRequest(ModelState.GetErrorsModelState()));
        }