I've got a working solution for this, but I'm wondering if this is the correct way to do it. Here's what I got so far.
I'm using ASP.Net Core 1.1.2 with ASP.NET Core Identity 1.1.2.
The important part in Startup.cs looks like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
app.UseFacebookAuthentication(new FacebookOptions
{
AuthenticationScheme = "Facebook",
AppId = Configuration["ExternalLoginProviders:Facebook:AppId"],
AppSecret = Configuration["ExternalLoginProviders:Facebook:AppSecret"]
});
}
FacebookOptionscomes with Microsoft.AspNetCore.Authentication.Facebook nuget package.
The callback function in AccountController.cs looks like this:
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
//... SignInManager<User> _signInManager; declared before
ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync();
SignInResult signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
byte[] thumbnailBytes = null;
if (info.LoginProvider == "Facebook")
{
string nameIdentifier = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
string thumbnailUrl = $"https://graph.facebook.com/{nameIdentifier}/picture?type=large";
using (HttpClient httpClient = new HttpClient())
{
thumbnailBytes = await httpClient.GetByteArrayAsync(thumbnailUrl);
}
}
//...
}
So this code is working absolutely fine but, as mentioned before, is this the correct way (technically, not opinion-based) to do it?
If you are trying to retrieve the Facebook User Details like Profile Picture, Email, Name, DOB and Gender, add the following lines in Configure method of the Startup class. Here, AppId and AppSecret are fed from Facebook App. If you are still confused, please refer to my previous article.
Once the login is successful with Facebook, you will be redirected to ExternalLoginConfirmation page which will display your Facebook details just like below. You can "Register Now" into your Web App. Get User Details like Email, Name, DOB, Gender and Profile Picture from Facebook.
ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.
For more information, see Introduction to authorization in ASP.NET Core. The Login form is displayed when users select the Log in link or are redirected when accessing a page that requires authentication. When the user submits the form on the Login page, the AccountController Login action is called.
To get profile picture from Facebook, you need to configure Facebook options and subscribe at OnCreatingTicket event from OAuth.
services.AddAuthentication().AddFacebook("Facebook", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = configuration.GetSection("ExternalLogin").GetSection("Facebook").GetSection("ClientId").Value;
options.ClientSecret = configuration.GetSection("ExternalLogin").GetSection("Facebook").GetSection("ClientSecret").Value;
options.Fields.Add("picture");
options.Events = new OAuthEvents
{
OnCreatingTicket = context =>
{
var identity = (ClaimsIdentity)context.Principal.Identity;
var profileImg = context.User["picture"]["data"].Value<string>("url");
identity.AddClaim(new Claim(JwtClaimTypes.Picture, profileImg));
return Task.CompletedTask;
}
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With