Starting with the Visual Studio Web API project template, Iām trying to add custom claims to the token created by the /Api/Account/ExternalLogin endpoint. I add them via the FacebookAuthenticationProvider.OnAuthenticated callback, but they are not saved until OAuthAuthorizationServerProvider.AuthorizationEndpointResponse() .
Note. I use a similar approach described by Rahul Nath in an ASP.NET Web API article and External Login - Social Network Authentication
the code
In my Startup.Auth.cs class class ConfigureAuth() method (which is called from the OwinStartup class Configuration() method), I added a callback function to the OnAuthenticated property to set one requirement, foo , with a value of bar :
var facebookAuthenticationProvider = new FacebookAuthenticationProvider() { OnAuthenticated = (context) => { context.Identity.AddClaim(new Claim("foo", "bar")); return Task.FromResult(0); } };
Then I add the FacebookAuthenticationProvider instance to the new FacebookAuthenticationOptions object:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions() { AppId = "XXXX", AppSecret = "YYYY", Provider = facebookAuthenticationProvider };
And pass this to the OWIN UseFacebookAuthentication() method:
app.UseFacebookAuthentication(facebookAuthenticationOptions);
results
If I set a breakpoint in the OnAuthenticated , I see that my user claim is being added, as well as a number of other statements (including a couple from the urn:facebook namespace). So far so good.
When I check my claims using the AuthorizationEndpointResponse() method of my OAuthAuthorizationServerProvider class after Facebook authentication, however, there are only two claims in the context.Identity.Claims collection:
All claims urn:facebook have been removed, as well as my usual claim foo . I assume that some other place in the pipeline is to recreate a claim using the barebone claims set, but I'm not sure where.
Thoughts?
facebook asp.net-web-api asp.net-identity asp.net-web-api2 owin-middleware
Jeremy caney
source share