Esempio n. 1
0
    public async Task <IActionResult> OnGetLinkLoginCallbackAsync()
    {
        var user = await _userManager.GetUserAsync(User);

        if (user == null)
        {
            return(NotFound($"Unable to load user with ID 'user.Id'."));
        }

        var info = await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString());

        if (info == null)
        {
            throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'.");
        }

        var result = await _userManager.AddLoginAsync(user, info);

        if (!result.Succeeded)
        {
            StatusMessage = "The external login was not added. External logins can only be associated with one account.";
            return(RedirectToPage());
        }

        await _userManager.RefreshExternalLoginFeatures(user);

        // Clear the existing external cookie to ensure a clean login process
        await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

        StatusMessage = "The external login was added.";
        return(RedirectToPage());
    }
    public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
    {
        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)
        {
            var user = new FantasyCriticUser {
                Id = Guid.NewGuid(), UserName = Input.DisplayName, Email = Input.Email
            };

            var result = await _userManager.CreateAsync(user);

            if (result.Succeeded)
            {
                var fullUser = await _userManager.FindByIdAsync(user.Id.ToString());

                result = await _userManager.AddLoginAsync(fullUser, info);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                    var confirmLink = await LinkBuilder.GetConfirmEmailLink(_userManager, fullUser, Request);

                    await _emailSendingService.SendConfirmationEmail(fullUser, confirmLink);

                    await _signInManager.SignInAsync(fullUser, 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());
    }