Permalink
Browse files

Read user's email address

  • Loading branch information...
ZainRizvi committed Mar 24, 2016
1 parent 11b4110 commit f771c75cc89691504a04e9b5b985428998002f8e
Showing with 3,253 additions and 15,979 deletions.
  1. +17 −0 src/FacebookAuthSite/Startup.cs
  2. +5 −2 src/FacebookAuthSite/project.json
  3. +3,231 −15,977 src/FacebookAuthSite/project.lock.json
@@ -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
{
@@ -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
@@ -34,8 +34,11 @@
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
"dnx451": {
"dependencies": {
"Facebook": "7.0.6"
}
}
},
"exclude": [
Oops, something went wrong.

16 comments on commit f771c75

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 2, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 2, 2016

Owner

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?

Owner

ZainRizvi replied Apr 2, 2016

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

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 2, 2016

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 2, 2016

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 2, 2016

@ZainRizvi

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 2, 2016

Owner

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.

Owner

ZainRizvi replied Apr 2, 2016

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

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 2, 2016

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

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

@ZainRizvi

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 3, 2016

Owner

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

Owner

ZainRizvi replied Apr 3, 2016

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

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 3, 2016

Owner

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

Owner

ZainRizvi replied Apr 3, 2016

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

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 5, 2016

Owner

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

Owner

ZainRizvi replied Apr 5, 2016

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

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 5, 2016

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 6, 2016

@DilipK01

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 8, 2016

@ZainRizvi

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 15, 2016

Owner

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?

Owner

ZainRizvi replied Apr 15, 2016

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

This comment has been minimized.

Show comment
Hide comment
@DilipK01

DilipK01 Apr 15, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@ZainRizvi

ZainRizvi Apr 15, 2016

Owner

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

Owner

ZainRizvi replied Apr 15, 2016

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.