Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

How to get additional fields using the Facebook provider in RC1? #783

Closed
gdoron opened this issue Apr 16, 2016 · 2 comments
Closed

How to get additional fields using the Facebook provider in RC1? #783

gdoron opened this issue Apr 16, 2016 · 2 comments

Comments

@gdoron
Copy link

gdoron commented Apr 16, 2016

I'm using ASP.NET core RC1 (and can't upgrade to the not yet release RC2 nightly builds because of the lack of VS support in RC2).

I'm trying to get additional fields from Facebook (first_name, last_name, email and significant_other).

I used the code suggested on this issue:

app.UseFacebookAuthentication(options =>
{
    options.AppId = Configuration["Authentication:Facebook:AppId"];
    options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
    options.Scope.Add("email");
    options.Scope.Add("user_relationships");
    options.BackchannelHttpHandler = new HttpClientHandler();
    options.UserInformationEndpoint = 
        "https://graph.facebook.com/v2.5/me?fields=id,email,first_name,last_name,significant_other";

This solution indeed returns the email of the user, but fails with first_name, last_name and significant_other (and any other field I tried besides name, id and email).

Also, is it possible getting the FB access token? We might need it for future querying of other edges, or use it to manually query Facebook because ASP.NET Core has a bug (at least in RC1).

I need a way, even if not the cleanest.

@kevinchalet
Copy link
Contributor

Cross-posted from SO: http://stackoverflow.com/questions/36669666/how-to-get-additional-fields-using-the-facebook-provider-in-asp-net-core-rc1


I'm trying to get additional fields from Facebook (first_name, last_name, email and significant_other). This solution indeed returns the email of the user, but fails with first_name, last_name and significant_other (and any other field I tried besides name, id and email).

In RC1, the Facebook middleware automatically stores the email as a claim, but not the first name or last name so you need to manually extract them using the event model if you want to be able to retrieve from application code:

app.UseFacebookAuthentication(options => {
    options.Events = new OAuthEvents {
        OnCreatingTicket = context => {
            var surname = context.User.Value<string>("last_name");
            context.Identity.AddClaim(new Claim(ClaimTypes.Surname, surname));

            return Task.FromResult(0);
        }
    };
});

In RC2, custom code won't be necessary, as the first name/last name are now included by default: #688.


Also, is it possible getting the FB access token? We might need it for future querying of other edges, or use it to manually query Facebook because ASP.NET Core has a bug (at least in RC1).

You can use the SaveTokensAsClaims option to store the access/refresh tokens as claims (enabled by default in RC1). If you need more information about this feature, you can take a look the PR that introduced it: #257.

app.UseFacebookAuthentication(options => {
    options.SaveTokensAsClaims = true;
});

You can retrieve it like any other claim:

var token = User.FindFirst("access_token")?.Value

Note: in RC2, this feature was revamped and tokens won't be stored in claims but in authentication properties: #698.

@gdoron
Copy link
Author

gdoron commented Apr 16, 2016

Yes, sorry for the cross posting.
I needed it urgently for tomorrow morning, and already Googled for 3 days or so. 😢
Tons of non-working answers out there.

@gdoron gdoron closed this as completed Apr 16, 2016
@smbecker smbecker mentioned this issue May 2, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants