MVC 4 oAuth With Facebook does not return email address

二次信任 提交于 2019-12-06 01:51:40

In order for Facebook to return an e-mail you must specify it in the scope. For example with the JS SDK you would do something like....

return FB.login(function(response) {
    if (response.authResponse) {
      return window.location = '/auth/facebook/callback';
    }
  }, {scope: 'email,user_likes'}
);

It is because Facebook allows its users to specify what data is shared with 3rd party authentication services, such as you website Victor. And by default, I believe the Email addresses (yes, there are multiple you can add to a facebook account) are not shared byd default. A user has to go in and explicitly share their email addresses with 3rd parties - something I don't think anyone ever uses.

The only "mandatory" shared data that Facebook forces shared is the FullName (e.g. "Eric Duncan"). Everything else is optional for the user to share.

For example, I have my Facebook setup to share only my birthdate, but not the birthyear. I also have my Facebook setup to share my interests but not my email.

Therefore, you have to code for what is guaranteed - the display name in Facebook's case (which is called the "UserName" in the MVC 4.0 OAuth). Everything else is not guaranteed.

What I do in my OAuth applications, including Facebook connect, is after a successful authentication with the 3rd party, on the callback url page (e.g. ExternalLoginConfirmation.cshtml in MVC 4.0), I will ask the user to enter their Display Name, email address, birthdate, etc. And make those fields required using standard data annotations on your custom view model.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!