skip to Main Content

I am trying to receive more information about the user after successful authentication with facebook in the

public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)

method of the Account controller.

How am I able to receive the facebook access_token, so that I can use the graph api? I think I may miss something obvious but unfortunately cannot find what I am missing..

Thank you very much for your help!

2

Answers


  1. Set SaveTokens to true in options.

    var facebookOptions = new FacebookOptions
    {
          AppId = "",
          AppSecret = "",
          SaveTokens = true,
          ...          
    };
    
    app.UseFacebookAuthentication(facebookOptions);
    

    And get it:

    HttpContext.Authentication.GetTokenAsync("access_token");
    

    Keep in mind to use GetTokenAsync you need to add using Microsoft.AspNetCore.Authentication;

    Login or Signup to reply.
  2. Access Token may be stored in auth properties (OAuthOptions.SaveTokens option enable/disable this behavior, false by default).

    Use AuthenticationTokenExtensions class to get token from AuthenticationProperties:

    public static string GetTokenValue(this AuthenticationProperties properties, string tokenName)
    public static IEnumerable<AuthenticationToken> GetTokens(this AuthenticationProperties properties)
    

    For example, if you use cookie authentication:

    var authInfo = await HttpContext.Authentication.GetAuthenticateInfoAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    string accessToken = authInfo.Properties.GetTokenValue("access_token");
    

    Notice, that before RC2 tokens were stored in claims (related PR), so you may find old code like next one, that doesn’t work

    var token = User.FindFirst("access_token")?.Value
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search