예제 #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 <IActionResult> SignIn(SignInViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, AccountContent.InvalidUsernameErrorText);
                return(View(model));
            }

            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                ModelState.AddModelError(string.Empty, AccountContent.EmailConfirmationRequiredErrorText);
                return(View(model));
            }

            var signInResult = await _signInManager.PasswordSignInAsync(model.Email, model.Password, true, false);

            if (signInResult.Succeeded)
            {
                _logger.LogInformation(1, "User logged in.");
                return(Redirect(string.IsNullOrWhiteSpace(model.ReturnUrl) ? "/" : model.ReturnUrl));
            }

            if (signInResult.IsNotAllowed)
            {
                _logger.LogWarning(2, "The user account is not activated");
                await SendActivationEmail(user, model.ReturnUrl);

                return(RedirectToAction(nameof(RegistrationComplete), new
                {
                    protectedSequence = _protector.Protect(_urlEncoderWrapper.UrlEncode(model.Email))
                }));
            }

            if (signInResult.IsLockedOut)
            {
                _logger.LogWarning(2, "User account locked out.");
                return(View("Lockout"));
            }

            ModelState.AddModelError(string.Empty, AccountContent.InvalidLoginAttemptText);
            return(View(model));
        }
 public async Task <BaseIdentityUser> FindByEmailAsync(string email)
 {
     return(await _userManager.FindByEmailAsync(email));
 }