public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName            = Input.Email,
                    Email               = Input.Email,
                    FirstName           = Input.FirstName,
                    LastName            = Input.LastName,
                    FullName            = Input.FirstName + " " + Input.LastName,
                    SendEventNewsletter = Input.SendEventNewsletter
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _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 },
                        protocol: Request.Scheme);

                    // Send the email
                    string apiKey    = Configuration?.GetEmailSettings("apiKey");
                    string apiSecret = Configuration?.GetEmailSettings("apiSecret");
                    int    contactId = await _emailSender.CreateContactAsync(apiKey, apiSecret, user.FirstName + " " + user.LastName, user.Email);

                    if (contactId != 0)
                    {
                        user.ContactId = contactId;
                        long listRecipientId = await _emailSender.AddContactToContactListAsync(apiKey, apiSecret, contactId.ToString(CultureInfo.InvariantCulture), "12508");

                        user.ListRecipientId = listRecipientId;
                        await _userManager.UpdateAsync(user);
                    }

                    await _emailSender.SendMailjetAsync(apiKey, apiSecret, 1080920, "Bitte bestätige deine Emailadresse", "*****@*****.**", "Das Großstadt Dinner Team", Input.FirstName, Input.Email, Input.FirstName + " " + Input.LastName, callbackUrl);

                    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 async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl ??= Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }
            if (result.IsLockedOut)
            {
                return(RedirectToPage("./Lockout"));
            }
            if (result.IsNotAllowed)
            {
                _logger.LogWarning("User not allowed.");
                return(RedirectToPage("RegisterConfirmation"));
            }
            else
            {
                // If the user does not have an account, then ask the user to create an account.
                ReturnUrl     = returnUrl;
                LoginProvider = info.LoginProvider;
                if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    var nameSplitted = info.Principal.Identity.Name.Split(" ");
                    Input = new InputModel
                    {
                        Email          = info.Principal.FindFirstValue(ClaimTypes.Email),
                        ProfilePicture = info.Principal.FindFirstValue(JwtClaimTypes.Picture),
                        FirstName      = nameSplitted[0],
                        LastName       = nameSplitted[1]
                    };
                }

                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser {
                        UserName            = Input.Email,
                        Email               = Input.Email,
                        SendEventNewsletter = true,
                        ProfilePicture      = Input.ProfilePicture,
                        FirstName           = Input.FirstName,
                        LastName            = Input.LastName
                    };
                    var createResult = await _userManager.CreateAsync(user);

                    if (createResult.Succeeded)
                    {
                        createResult = await _userManager.AddLoginAsync(user, info);

                        if (createResult.Succeeded)
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            _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, code },
                                protocol: Request.Scheme);

                            // Send the email
                            string apiKey    = Configuration?.GetEmailSettings("apiKey");
                            string apiSecret = Configuration?.GetEmailSettings("apiSecret");
                            int    contactId = await _emailSender.CreateContactAsync(apiKey, apiSecret, user.FirstName + " " + user.LastName, user.Email);

                            if (contactId != 0)
                            {
                                user.ContactId = contactId;
                                long listRecipientId = await _emailSender.AddContactToContactListAsync(apiKey, apiSecret, contactId.ToString(CultureInfo.InvariantCulture), "12508");

                                user.ListRecipientId = listRecipientId;
                                await _userManager.UpdateAsync(user);
                            }

                            await _emailSender.SendMailjetAsync(apiKey, apiSecret, 1080920, "Bitte bestätige deine Emailadresse", "*****@*****.**", "Das Großstadt Dinner Team", Input.FirstName, Input.Email, Input.FirstName + " " + Input.LastName, callbackUrl);

                            return(LocalRedirect(returnUrl));
                        }
                    }

                    foreach (var error in createResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }

                return(Page());
            }
        }