public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); IsRegistrationOver = await _mediator.Send(new RegistrationAvailabilityQuery()); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, SkaterName = Input.SkaterName, AcceptProgressNotifications = Input.AcceptProgressNotifications, Target = Input.Target }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); var fromSkateEverywhere = Request.Cookies.Any(x => x.Key.Equals("FromSkateEverywhere") && x.Value.Equals("true")); var command = new SendRegistrationEmailCommand { Email = Input.Email, EmailConfirmationUrl = callbackUrl, FromSkateEverywhere = fromSkateEverywhere }; await _mediator.Send(command); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public void Given_A_Valid_Command_Should_Invoke_Send_Method_Once() { // Arrange var command = new SendRegistrationEmailCommand { Username = "******", Email = "*****@*****.**", CallBackUrl = "http://callbackurl" }; _emailService.Send(Arg.Any <EmailMessage>()); // Act _sut.Handle(command, CancellationToken.None); // Assert _emailService.Received(1).Send(Arg.Any <EmailMessage>()); }
public void Given_A_Valid_Command_Should_Send_Registration_Email_Successfully() { // Arrange var command = new SendRegistrationEmailCommand { Username = "******", Email = "*****@*****.**", CallBackUrl = "http://callbackurl" }; _emailService.Send(Arg.Any <EmailMessage>()); // Act Action act = () => _sut.Handle(command, CancellationToken.None); // Assert act.Should().NotThrow(); }
Given_A_Valid_Command_If_Connection_To_EmailServer_Fails_Should_Handle_ServiceNotConnected_Exception_And_Return_Errors() { // Arrange var command = new SendRegistrationEmailCommand { Username = "******", Email = "*****@*****.**", CallBackUrl = "http://callbackurl" }; _emailConfiguration.SmtpUsername.Returns("*****@*****.**"); _emailService.When(x => _emailService.Send(Arg.Any <EmailMessage>())) .Do(x => throw new ServiceNotConnectedException()); // Act var result = await _sut.Handle(command, CancellationToken.None); // Assert result.Errors.Should().NotBeEmpty(); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { IsRegistrationOver = await _mediator.Send(new RegistrationAvailabilityQuery()); returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid && !IsRegistrationOver) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, IsStravaAccount = info.LoginProvider.Equals("Strava", System.StringComparison.CurrentCultureIgnoreCase), AcceptProgressNotifications = Input.AcceptProgressNotifications, Target = Input.Target }; if (!info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.Email)) && info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.NameIdentifier))) { user.UserName = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier); } if (info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.Name))) { user.SkaterName = info.Principal.FindFirstValue(ClaimTypes.Name); } else if (info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.GivenName)) && info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.Surname))) { user.SkaterName = string.Format("{0} {1}", info.Principal.FindFirstValue(ClaimTypes.GivenName), info.Principal.FindFirstValue(ClaimTypes.Surname)).Trim(); } else if (info.Principal.HasClaim(x => x.Type.Equals(ClaimTypes.GivenName))) { user.SkaterName = info.Principal.FindFirstValue(ClaimTypes.GivenName); } var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = userId, code = code }, protocol: Request.Scheme); var fromSkateEverywhere = Request.Cookies.Any(x => x.Key.Equals("FromSkateEverywhere") && x.Value.Equals("true")); var command = new SendRegistrationEmailCommand { Email = Input.Email, EmailConfirmationUrl = callbackUrl, FromSkateEverywhere = fromSkateEverywhere }; await _mediator.Send(command); // If account confirmation is required, we need to show the link if we don't have a real email sender if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email })); } await _signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } ProviderDisplayName = info.ProviderDisplayName; ReturnUrl = returnUrl; return(Page()); }