如何使用 ASP.NET Identity (OWIN) 访问 Facebook 私人信息? [英] How to access Facebook private information by using ASP.NET Identity (OWIN)?

查看:29
本文介绍了如何使用 ASP.NET Identity (OWIN) 访问 Facebook 私人信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ASP.NET MVC 5 中开发一个网站(目前使用 RC1 版本).该网站将使用 Facebook 进行用户身份验证和检索初始个人资料数据.

I am developing a web site in ASP.NET MVC 5 (using RC1 version currently). The site will use Facebook for user authentication and for retrieving initial profile data.

对于身份验证系统,我使用了新的基于 OWIN 的 ASP.NET 身份引擎 (http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx),因为它极大地简化了与外部提供商进行身份验证的过程.

For the authentication system I am using the new OWIN based ASP.NET Identity engine (http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx), since it greatly simplifies the process of authenticating with external providers.

问题是,一旦用户首次登录,我想从 Facebook 个人资料中获取其电子邮件地址,但此数据未包含在生成的声明中.所以我想了这些替代方法来获取地址:

The problem is that once a user first logs in, I want to get its email address from the Facebook profile, but this data is not included in the generated claims. So I have thought on these alternatives to get the address:

  1. 指示 ASP.NET Identity 引擎将电子邮件地址包含在从 Facebook 检索然后转换的数据集索赔.我不知道这是否可能.

  1. Instruct the ASP.NET Identity engine to include the email address in the set of data that is retrieved from Facebook and then converted to claims. I don't know if this is possible at all.

使用 Facebook 图 API(https://developers.facebook.com/docs/getting-started/graphapi) 到使用 Facebook 用户 ID(这是包含在索赔数据中).但是如果用户有,这将不起作用将他的电子邮件地址设为私密.

Use the Facebook graph API (https://developers.facebook.com/docs/getting-started/graphapi) to retrieve the email address by using the Facebook user id (which is included in the claims data). But this will not work if the user has set his email address as private.

使用 Facebook 图形 API,但指定我"而不是脸书用户ID(https://developers.facebook.com/docs/reference/api/user).但是一个访问令牌是必需的,我不知道如何(或者如果它是可能)检索 ASP.NET 用于的访问令牌获取用户数据.

Use the Facebook graph API, but specifying "me" instead of the Facebook user id (https://developers.facebook.com/docs/reference/api/user). But an access token is required, and I don't know how to (or if it's possible at all) retrieve the access token that ASP.NET uses to obtain the user data.

所以问题是:

  1. 如何指示 ASP.NET Identity 引擎检索来自 Facebook 的其他信息并将其包含在索赔中数据?

  1. How can I instruct the ASP.NET Identity engine to retrieve additional information from Facebook and include it in the claims data?

或者,我如何检索生成的访问令牌以便我可以问 Facebook 自己吗?

Or alternatively, how can I retrieve the generated access token so that I can ask Facebook myself?

谢谢!

注意:对于身份验证系统,我的应用程序使用基于此 SO 答案中链接的示例项目的代码:https://stackoverflow.com/a/18423474/4574

Note: for the authentication system my application uses code based on the sample project linked in this SO answer: https://stackoverflow.com/a/18423474/4574

推荐答案

要从 facebook 检索其他信息,您可以指定在配置 facebook 身份验证选项时要包含的范围.可以通过实现提供者的 OnAuthenticated 方法来获取检索到的附加信息:

To retrieve additional information from facebook you can specify scopes you would like to include when you configure the facebook authentication options. Getting the additional information that's retrieved can be achieved by implementing the provider's OnAuthenticated method like this:

var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
    Provider = new FacebookAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
            {
                // All data from facebook in this object. 
                var rawUserObjectFromFacebookAsJson = context.User;

                // Only some of the basic details from facebook 
                // like id, username, email etc are added as claims.
                // But you can retrieve any other details from this
                // raw Json object from facebook and add it as claims here.
                // Subsequently adding a claim here will also send this claim
                // as part of the cookie set on the browser so you can retrieve
                // on every successive request. 
                context.Identity.AddClaim(...);

                return Task.FromResult(0);
            }
    }
};

//Way to specify additional scopes
facebookOptions.Scope.Add("...");

app.UseFacebookAuthentication(facebookOptions);

根据代码这里我看到电子邮件是如果 facebook 已发送,则已在此处检索并添加为声明.你看不到吗?

Per the code here i see the email is already retrieved and added as a claim here if facebook has sent. Are you not able to see it?

这篇关于如何使用 ASP.NET Identity (OWIN) 访问 Facebook 私人信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆