Starting with Visual Studio's "Web API" project template, I am attempting to add custom claims to the token created by the /Api/Account/ExternalLogin
endpoint. I add these via the FacebookAuthenticationProvider.OnAuthenticated
callback, but they do not persist through to the OAuthAuthorizationServerProvider.AuthorizationEndpointResponse()
.
Note: I am using a similar approach as documented by Rahul Nath in his article ASP.NET Web API and External Login - Authenticating with Social Networks
Code
In my Startup.Auth.cs
class's ConfigureAuth()
method (which is called from the OwinStartup
class's Configuration()
method) I added a callback function to the OnAuthenticated
property in order to set a single claim, foo
, with the value bar
:
var facebookAuthenticationProvider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new Claim("foo", "bar"));
return Task.FromResult(0);
}
};
I then add the FacebookAuthenticationProvider
instance to a new FacebookAuthenticationOptions
object:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = "XXXX",
AppSecret = "YYYY",
Provider = facebookAuthenticationProvider
};
And pass that onto OWIN's UseFacebookAuthentication()
method:
app.UseFacebookAuthentication(facebookAuthenticationOptions);
Results
If I put a breakpoint in the OnAuthenticated
callback I can see that my custom claim is being added, as are a number of other claims (including a couple from the urn:facebook
namespace). So far so good.
When I examine my claims via the AuthorizationEndpointResponse()
method of my OAuthAuthorizationServerProvider
class after a Facebook authentication, however, there are only two claims available in the context.Identity.Claims
collection:
- http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier
- http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
All of the urn:facebook
claims have been removed, as has my custom foo
claim. I'm assuming some other location in the pipeline is recreating the identity with a barebones set of claims, but I am not sure where.
Thoughts?