简体   繁体   中英

Get Facebook OAuth token and store in claims when using WebApi

Ive been following this blog which shows how to get additional Facebook data from a user using the scope and then storing it in claims.

http://blogs.msdn.com/b/webdev/archive/2013/10/16/get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx

This seems to work fine when in a standard MVC web application. However I am struggling getting the token stored when using WebApi. The Startup.Auth code is exactly the same in my project and I can see the scope being requested is correct, but I dont understand at what point I should have access to the requested data. Also, the claim never seems to get stored which is odd.

I recommend you to read my blog post about ASP.NET Web API 2 external logins with Facebook and Google which walkthroughs you on how to integrate external logis with Web API without using MVC dependcnies or templates.

Any how if you need to get Facebook token you need to override the on Authenticated method and store it on custom claim so it will be available in claims dictuinatory as the code below:

   public class FacebookAuthProvider : FacebookAuthenticationProvider
    {
        public override Task Authenticated(FacebookAuthenticatedContext context)
        {
            context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
            return Task.FromResult<object>(null);
        }
    }

And you call this class here in the Startup.cs:

 //Configure Facebook External Login
            facebookAuthOptions = new FacebookAuthenticationOptions()
            {
                AppId = "xxxxxx",
                AppSecret = "xxxxxx",
                Provider = new FacebookAuthProvider()
            };
            app.UseFacebookAuthentication(facebookAuthOptions);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM