【问题标题】:Azure App Service FacebookAzure 应用服务 Facebook
【发布时间】:2017-06-23 21:09:09
【问题描述】:

我已从 Azure 移动服务迁移到应用服务,但我很难弄清楚如何最好地实施扩展的 Facebook 身份验证。

在我的旧实现中,我从 FacebookLoginProvider 继承并从声明中获取令牌。然后我将 CustomFacebookLoginProvider 添加到我的登录提供程序中。然后我使用令牌来获取有关用户的更多信息(他们的出生日期、朋友和性别)。有了这些信息,我创建了一个用户对象并将其保存到我的数据库中。

有没有人对如何最好地在应用服务中重新创建它有任何建议,因为我找不到任何文档。

【问题讨论】:

    标签: facebook azure azure-app-service-plans azure-app-service-envrmnt


    【解决方案1】:

    至于如何设置 Facebook 身份验证,您可以在此处找到文档(听起来您已经弄清楚了这么多):

    https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-how-to-configure-facebook-authentication/

    Facebook认证设置完毕,可以参考以下获取用户信息的方法:

    https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#user-info

    // Get the credentials for the logged-in user.
    var credentials = 
        await this.User
        .GetAppServiceIdentityAsync<FacebookCredentials>(this.Request);
    
    if (credentials.Provider == "Facebook")
    {
        // Create a query string with the Facebook access token.
        var fbRequestUrl = "https://graph.facebook.com/me?access_token=" 
            + credentials.AccessToken;
    
        // Create an HttpClient request.
        using (var client = new System.Net.Http.HttpClient())
        {
            // Request the current user info from Facebook.
            using (var resp = await client.GetAsync(fbRequestUrl))
            {
                resp.EnsureSuccessStatusCode();
    
                // Do something here with the Facebook user information.
                var fbInfo = await resp.Content.ReadAsStringAsync();
            }
        }
    }
    

    请注意,您必须为 System.Security.Principal 添加一个 using 语句,以使 GetAppServiceIdentityAsync 扩展方法起作用。

    有关您可以查询哪些 Facebook 用户属性的更多信息,请参阅此处的 Facebook 文档:https://developers.facebook.com/docs/graph-api/reference/user。请注意,您可能需要在调用 Facebook 图表时指定您想要的用户属性作为附加 fields 查询字符串参数。

    【讨论】:

    【解决方案2】:

    从移动服务切换到移动应用程序时,我必须做的唯一更改是将开发人员门户中回调 URL 的末尾更改为使用 /.auth/login/facebook/callback 而不是 /signin-facebook 和它工作方式与以前完全相同。

    请注意,这是针对具有 .NET 后端的 Windows 应用程序;您没有指定您使用的是什么,因此您的里程可能会有所不同。

    【讨论】:

    • 这种方法确实按预期工作,但我无法获取 Facebook 令牌以查询 Facebook 以获取有关用户的更多信息。
    • iOS,但客户端应该不重要,因为这应该发生在后端。
    • 我相信我们在 MobileServiceClient 实例上使用 InvokeApiAsync 来获取用户信息,但那是 C#。我不是为 iOS 开发的,所以我认为我无法提供任何进一步的帮助,抱歉。 :(
    【解决方案3】:

    我一直在使用以下方法在 iOS 应用中获取 Facebook 访问令牌。

    应用服务在请求标头中包含 Facebook 访问令牌,请参阅 https://azure.microsoft.com/en-in/documentation/articles/app-service-api-authentication/

    要获取访问令牌,请在 Azure 门户中创建自定义 API,例如facebookUserInfo,代码如下:

    module.exports = {
    "get": function (request, response, next) {
        response.send(200, { facebookAccessToken: request.headers['x-ms-token-facebook-access-token'] });
    }};        
    

    在 iOS 应用中,使用以下代码查询自定义 API:

        let client = self.table!.client
        if client.currentUser != nil {
            client.invokeAPI("facebookUserInfo", body: nil, HTTPMethod: "GET", parameters: nil, headers: nil, completion: { (result, response, error) -> Void in
                if let resultDict = result {
                    if let facebookAccessToken = resultDict["facebookAccessToken"]! {
                        print(facebookAccessToken)
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      通过使用 Azure App Services 的 Easy Auth 功能,我无需担心身份验证。 我对此有一篇博文。我已经解释了我们如何使用 FB GraphApi 来查询 FB 数据。这是链接:https://blogs.msdn.microsoft.com/kaushal/2017/06/08/using-easy-auth-to-query-facebook-information-via-graph-api/

      我在 Github 上部署了示例代码。这是链接:https://github.com/kaushalp/Facebook-GraphApi-with-EasyAuth

      【讨论】:

        猜你喜欢
        • 2018-02-20
        • 1970-01-01
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        • 2019-03-29
        相关资源
        最近更新 更多