0

I am learning Identity and OWIN. I have already implemented Google and Facebook login. Now i am trying to implement Microsoft login and

  • Email is null
  • No personal information like first name, last name, location etc

Facebook had the similar issue and following helped with getting the data:

Access email address in the OAuth ExternalLoginCallback from Facebook v2.4 API in ASP.NET MVC 5

and

https://developers.facebook.com/docs/graph-api/reference/user

How can i get the data back from Microsoft?

enter image description here

Community
  • 1
  • 1
learning...
  • 3,104
  • 10
  • 58
  • 96

1 Answers1

1

For email, you need to add the scope "wl.emails". To access the rest of the data, you need to add claims so that you can get at it from your controller.

In Startup.Auth.cs you need something like:

var msaccountOptions = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationOptions()
{
    ClientId = /* client id */,
    ClientSecret = /* client secret */,
    Provider = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
        {
            context.Identity.AddClaim(new System.Security.Claims.Claim("urn:microsoftaccount:access_token", context.AccessToken, XmlSchemaString, "Microsoft"));
            foreach (var x in context.User)
            {
                var claimType = string.Format("urn:microsoftaccount:{0}", x.Key);
                string claimValue = x.Value.ToString();
                if (!context.Identity.HasClaim(claimType, claimValue))
                    context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Microsoft"));

            }

            return Task.FromResult(0);
        }
    }
};
msaccountOptions.Scope.Add("wl.emails");
app.UseMicrosoftAccountAuthentication(msaccountOptions);

Then, in your controller action, you can access the user details like:

var identity = await HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var firstNameClaim = identity.Claims.FirstOrDefault(c => c.Type.EndsWith("first_name"));
var firstName = firstNameClaim != null ? firstNameClaim.Value : null;

You'll need to consult the API documentation to determine the proper claim values for the service you're using.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444