This project has moved. For the latest updates, please go here.

Getting first name, last name and other details from facebook and google login

Nov 23, 2014 at 9:20 AM
Edited Nov 23, 2014 at 9:27 AM
I would like to know how to get the first name and last name for facebook and google login using microsoft owin security 3.0.0.

I have implemented the following code to get the facebook first name and last name based on source code found online. However, i do not really understand how this work.
  1. Does UseFacebookAuthentication by default does not include FacebookAuthenticationProvider currently? Meaning that we have to configure the FacebookAuthenticationProvider as shown in the code below ourself? Or have i retrieve it in the wrong way?
  2. urn:facebook:first_name is used to get facebook first_name, how about getting google first_name? urn:google:first_name?
  3. Is there any more simpler and direct method to get first name and last name other than this?
Thank you.

Original Code (not working)
app.UseFacebookAuthentication(
             appId: "*",
             appSecret: "**");

To retrieve data,
var firstName = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:facebook:first_name").Value;
I am expecting something like this, but looks like it does not accept provider as parameter (not working)
 app.UseFacebookAuthentication(
           appId:"*",
           appSecret:"**",
           provider: "defaultFacebookProvider");

var firstName = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:facebook:first_name").Value;
What I found online Modified code to get facebook information (working)
var x = new FacebookAuthenticationOptions();
        x.Scope.Add("email");
        x.AppId = "*";
        x.AppSecret = "***";
        x.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = async context =>
            {
                context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                foreach(var claim in context.User)
                {
                    var claimType = string.Format("urn:facebook:{0}", claim.Key);
                    string claimValue = claim.Value.ToString();
                    if(!context.Identity.HasClaim(claimType, claimValue))
                        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));

                }

            }
        };

        x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
        app.UseFacebookAuthentication(x);


var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var email = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;
var firstName = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:facebook:first_name").Value;
var lastName = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == "urn:facebook:last_name").Value;