For anyone using Microsoft' OWIN Facebook Authentication extensions. You can extend the default FacebookAuthenticationProvider
to obtain any requested (and granted) claims to the current identity. Then register that customer provider in the middleware. A simple custom provider would look like this...
public class CustomFacebookOAuthProvider: FacebookAuthenticationProvider
{
public override Task Authenticated(FacebookAuthenticatedContext context)
{
if (context.User.IsNotNull() && context.Id.IsNotNullOrEmpty())
{
//ADD THE CLAIMS YOU WANT YOUR APP TO CONSUME
context.Identity.AddClaim(new Claim(Constants.ClaimsTypes.PhotoUrl
, "https://graph.facebook.com/{0}/picture?height={1}&width={1}".With(context.Id, Constants.UI.DEFAUL_PROFILE_PICTURE_DIMENS)));
}
return base.Authenticated(context);
}
}
The implementation is simple, we only need to override the Authenticated
method, which will be called once the OAuth provider (Facebook in this example) has authenticated the user and returned the user details (translate this to a call to the UserInfo endpoint).
The context.User
is a NewtonSoft.Json.JObject
which can be desrialized into your own class...
var user = context.User.ToObject<FacebookOAuthUser>();
Just make sure you create the FacebookOAuthUser
class with the required properties.
Once you have that provider in place, all you need to do register it...
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AuthenticationType = "Facebook",
Caption = "Sign-in with Facebook",
SignInAsAuthenticationType = signInAsType,
AppId = ApplicationSettings.FacebookAppID,
AppSecret = ApplicationSettings.FacebookAppSecret,
Provider = new CustomFacebookOAuthProvider()
});