public async Task <IActionResult> OnPostAsync(string captcha, string inviteCode, string email, string?returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
        if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
        {
            ModelState.AddModelError("captcha", "Captcha validation failed");
        }
        if (ModelState.IsValid)
        {
            if (Input is null)
            {
                throw new Exception("Input is null.");
            }
            var user = new ApplicationUser {
                UserName = email, Email = email
            };
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                _logger.LogInformation("User created a new account with password.");

                var newUserEvent = new NewUserRegisteredEvent(email,
                                                              Request.HttpContext.Connection.RemoteIpAddress !.ToString());

                await _dispatcher.Dispatch(newUserEvent);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var userId = user.Id;

                var emailConfirmationResult = await _userManager.ConfirmEmailAsync(user, code);

                if (!emailConfirmationResult.Succeeded)
                {
                    throw new InvalidOperationException($"Error confirming email for user with ID '{userId}':");
                }

                await _newMemberService.MemberSetupAsync(userId, Input.FirstName !, Input.LastName !, inviteCode !, email);

                _logger.LogInformation($"Adding user {user.Email} to Member Role");
                var roles = await _roleManager.Roles.ToListAsync();

                var memberRole = roles.FirstOrDefault(r => r.Name == "Member");
                if (memberRole != null)
                {
                    await _userRoleMembershipService.AddUserToRoleAsync(userId, memberRole.Id);
                }
                return(RedirectToRoute("/User/MyProfile"));
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

        // If we got this far, something failed, redisplay form
        return(Page());
    }
Пример #2
0
        public async Task SetsUpNewMember()
        {
            _repository.Setup(r => r.GetAsync(It.IsAny <InvitationByInviteCodeSpec>())).ReturnsAsync(_invitation);

            Member member = await _newMemberService.MemberSetupAsync(_userId, _firstName, _lastName, _inviteCode);

            Assert.Equal(_userId, member.UserId);
            Assert.Equal(_firstName, member.FirstName);
            Assert.Equal(_lastName, member.LastName);

            _userRoleMembershipService.Verify(u => u.AddUserToRoleByRoleNameAsync(_userId, _roleName), Times.Once);
            Assert.False(_invitation.Active);
        }
Пример #3
0
        public async Task SetsUpNewMember()
        {
            var memberResult = new Member();
            var memberId     = memberResult.Id;

            _invitationRepository.Setup(r => r.GetBySpecAsync(It.IsAny <InvitationByInviteCodeSpec>(), CancellationToken.None)).ReturnsAsync(_invitation);
            _memberRepository.Setup(r => r.GetByIdAsync(memberId, CancellationToken.None)).ReturnsAsync(memberResult);
            _memberRegistrationService.Setup(r => r.RegisterMemberAsync(_userId)).ReturnsAsync(memberResult);

            Member member = await _newMemberService.MemberSetupAsync(_userId, _firstName, _lastName, _inviteCode);

            Assert.Equal(_firstName, member.FirstName);
            Assert.Equal(_lastName, member.LastName);

            _userRoleMembershipService.Verify(u => u.AddUserToRoleByRoleNameAsync(_userId, _roleName), Times.Once);
            Assert.False(_invitation.Active);
        }