I have a .net core 2.0 app and am implementing external login providers like google, twitter, and facebook. I have the requirement to get the user’s display name and profile picture, and can’t find any documentaion of how to achieve this in .net core 2.0.
I add the authentication like this post: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/
Here are my twitter login and callback functions…
[HttpGet]
[Route("/api/security/login/type/socialmedia/twitter")]
public IActionResult GetTwitterLogin(string redirect_uri)
{
ClientCallback = redirect_uri;
string redirectUrl = "/api/security/login/type/socialmedia/twittercallback";
var properties = SignInManager.ConfigureExternalAuthenticationProperties("Twitter", redirectUrl);
return Challenge(properties, "Twitter");
}
[HttpGet]
[Route("/api/security/login/type/socialmedia/twittercallback")]
public async Task<HttpResponseMessage> GetTwitterCallBackAsync()
{
var info = await SignInManager.GetExternalLoginInfoAsync();
var result = await SignInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (result.Succeeded)
{
}
else
{
}
Response.StatusCode = (int)HttpStatusCode.OK;
return null;
}
It looks like you can get some items from info.Principal.Claims, but nothing for the user’s display name or profile picture.
How do you get the display name or profile picture for the various login providers?
2
Answers
I finally figured this out...you need to add claims when you configure the authentication. These claims look at the resulting json response and pulls items from it. The pertinent lines are the ClaimActions items.
After getting the login information in your callback using
If populated successfully you can query the claims and find the values
Facebook images are different from google and twitter and can be found using...
In ASP.NET Core 2.0, FacebookOptions uses extension methods on ClaimActions to map the profile data returned by UserInformationEndpoint.
In the mapping above, “birthday” is a top-level property in the Facebook Graph API response that’s mapped to the value represented by the claim
ClaimTypes.DateOfBirth
.To grab the profile picture you would do the same thing, but since the picture in the Graph API response is a nested JSON object, you would have to use
MapCustomJson()
Here,
claim
is a NewtonSoftJObject
that usesJPath
syntax to select the nested property value and cast it to a string.The profile picture URL will now appear in your
Claims
list.