Exemplo 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());
    }