Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the FacebookAccessToken in the AspNet.Identity version 3 / ASP.Net MVC6 / VNext?

Previously, I could call info.ExternalIdentity.FindAll("FacebookAccessToken").

In AspNet.Identity version 3, I can't find the access token stored anywhere using the SignInManager. I also can't use FindAll anymore from ExternalIdentity.

Can I still retrieve the Facebook Access Token within my ASP.Net MVC6 project?

like image 831
Jonas Arcangel Avatar asked Jan 30 '26 16:01

Jonas Arcangel


1 Answers

You can access the access token by including it in the claims collection.

You have to tap into the mapping of the Facebook response to the ClaimsIdentity object via the FacebookAuthenticationNotifications OnAuthenticated event. In the Startup.cs file:

services.Configure<FacebookAuthenticationOptions>(options =>
{
    options.AppId = Configuration["Authentication:Facebook:AppId"];
    options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
    options.Scope.Add("user_birthday");
    options.Notifications = new FacebookAuthenticationNotifications
    {
        OnAuthenticated = async context => {

            var accessToken = context.AccessToken;
            var identity = (System.Security.Claims.ClaimsIdentity)context.Principal.Identity;
            identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
        }
    };
});

Then you will be able to find the access token in the ExternalIdentity.

like image 154
Lybecker Avatar answered Feb 01 '26 15:02

Lybecker