Skip to content

Commit

Permalink
Read user's email address
Browse files Browse the repository at this point in the history
  • Loading branch information
ZainRizvi committed Mar 24, 2016
1 parent 11b4110 commit f771c75
Show file tree
Hide file tree
Showing 3 changed files with 3,253 additions and 15,979 deletions.
17 changes: 17 additions & 0 deletions src/FacebookAuthSite/Startup.cs
Expand Up @@ -11,6 +11,9 @@
using Microsoft.Extensions.Logging;
using FacebookAuthSite.Models;
using FacebookAuthSite.Services;
using Microsoft.AspNet.Authentication.OAuth;
using Facebook;
using System.Security.Claims;

namespace FacebookAuthSite
{
Expand Down Expand Up @@ -97,6 +100,20 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
options.Scope.Add("public_profile");
options.Scope.Add("email");
options.Events = new OAuthEvents
{
OnCreatingTicket = context => {
// Use the Facebook Graph Api to get the user's email address
// and add it to the email claim
var client = new FacebookClient(context.AccessToken);
dynamic info = client.Get("me", new { fields = "name,id,email" });
context.Identity.AddClaim(new Claim(ClaimTypes.Email, info.email));
return Task.FromResult(0);
}
};
});

// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
Expand Down
7 changes: 5 additions & 2 deletions src/FacebookAuthSite/project.json
Expand Up @@ -34,8 +34,11 @@
},

"frameworks": {
"dnx451": { },
"dnxcore50": { }
"dnx451": {
"dependencies": {
"Facebook": "7.0.6"
}
}
},

"exclude": [
Expand Down

16 comments on commit f771c75

@DilipK01
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Zain,
I did exactly what you described. On the Facebook login page, I gave permission to use my email. However, I still don't get my email from Facebook. I just get Id and Name claims.

info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email) returns null.

Any idea/suggestion?

Thanks.
Dilip Kumar

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Dilip,

I take it you made the changes in this checkin (the "client.get("me"...) part), right?

If you put a breakpoint on that line, what does the info object returned by the FacebookClient contain?

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 2, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 2, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 2, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, can you try this:

Clone this repository, try syncing it to this change list, add your facebook secrets to it, and try running it. See if you get the email address then. That'll at least help tell if the problem is in your code or if it's something about how facebook is configured.

@DilipK01
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't know how to "clone" and "sync". I haven't done it before. Any suggestion is appreciated.

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you pretty new to using git/github? if so, no worries, we're all noobs at some point. I'm curious, how much programming do you know and what are you trying to learn/do?

Cloning a repository

To clone a repo/code base means to make a copy of it. You can easily clone any github repo (short for code repository) by going to the project page (in this case https://github.com/ZainRizvi/ASP.NET-Core-FacebookAuth) and looking for the https://github.com/[repository] link.

In your case it'll look like this (there's even a handy copy to clipboard button on the right side):
repository

Then you open up a command window on your computer, navigate to a folder where you want to save the copied the code to, and run git clone <repository link>.

Here, it would be git clone https://github.com/ZainRizvi/ASP.NET-Core-FacebookAuth.git

That'll create a folder ASP.NET-Core-FacebookAuth with all the code in it.

Syncing to a specific commit

Now to sync your code to this commit cd to the ASP.NET-Core-FacebookAuth directory. There run
git reset --soft <commit number>.

You can find the commit number at the top of this page (it's f771c75). So you'll run git reset --soft f771c75cc89691504a04e9b5b985428998002f8e

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you can even "fork" my code (which here means make your own online copy of it on github) by again navigating to the project's root directory at https://github.com/ZainRizvi/ASP.NET-Core-FacebookAuth and clicking the "Fork" button on the top right corner:

Fork project

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DilipK01, did you have any luck following the steps?

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 5, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 6, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DilipK01
Copy link

@DilipK01 DilipK01 commented on f771c75 Apr 8, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dilip, were you able to get it to work?

Just so you know, none of your screen shots show up. If you want to attach a picture can you try doing it in the github conversation directly rather than by replying to the email github sends you?

@DilipK01
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Zain,

Finally, I was able to successfully run your project on my machine. However, I still don’t get my email from Facebook. The screenshot at the break-point is attached herewith.

I really appreciate your help and guidance.

Dilip Kumar
2016-04-07

@ZainRizvi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you didn't authorize Facebook to give your app your email address when you first logged on.

I'd suggest going to Facebook, go to the security settings, delete your app from the authorized apps list, and then try registering your facebook account from within your app once again. That'll invoke Facebook's "Allow xxx app access to your info?" flow again, and this time it'll get access to your email address I'd everything is setup correctly

Please sign in to comment.