PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 16, 2022

[FIXED] How to request additional claims with Facebook authentication

 July 16, 2022     asp.net-core, asp.net-identity, authentication, c#, facebook-login     No comments   

Issue

I have created a new ASP.net Core 3.0 website, with individual user authentication from the .net project template.

I am storing users by registering directly on the site or using facebook. Here's what my Startup.cs looks like:

public void ConfigureServices(IServiceCollection services)
{
     services.Configure<CookiePolicyOptions>(options => {
           // This determines user consent for non-essential cookies is needed for a given request.
           options.CheckConsentNeeded = context => true;
           options.MinimumSameSitePolicy = SameSiteMode.None;
      });

      services.AddDbContext<ApplicationDbContext>(options => {
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
      });

      services.AddAuthentication()
           //.AddMicrosoftAccount(microsoftOptions => {  })
           //.AddGoogle(googleOptions => {  })
           //.AddTwitter(twitterOptions => {  })
           .AddFacebook(facebookOptions =>
           {
               facebookOptions.AppId = "x";
               facebookOptions.AppSecret = "y";
           });

    ...
}

This all works fine and using the default template I can Register/Login as expected.

When I login via Facebook, I'm given five default pieces of claims information about the user:

  • name identifier
  • email address
  • given name
  • name
  • surname

What I need to do is extend the code so it does more than the default and gives me access to the users:

  • phone number
  • address
  • postcode

(Obviously with the users consent)

I've been reading the documentation (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/facebook-logins?view=aspnetcore-3.1) but it seems to be more about configuring the authentication provider than additional claims info.

Has anyone done this? Maybe it's not possible?

Thanks for any pointers in advance.


Solution

From: Persist additional claims and tokens from external providers in ASP.NET Core - If the app requires additional scopes, add them to the options. For example, in Facebook, you can add scopes like this.

services.AddAuthentication().AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = "4387237897237";
    facebookOptions.AppSecret = "23498423808320849082308";
    facebookOptions.Scope.Add("email");
    facebookOptions.Scope.Add("user_location");
    facebookOptions.Scope.Add("user_birthday");
});

Which will show details like this - login screen more details.

Facebook authentication dialog



Answered By - Anuraj
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing