【问题标题】:How to "rerequest" Facebook permissions using ASP.NET Identity如何使用 ASP.NET 身份“重新请求”Facebook 权限
【发布时间】:2017-02-02 16:03:24
【问题描述】:

有没有人成功地使用 ASP.NET 启用 Facebook 的重新身份验证功能? (https://developers.facebook.com/docs/facebook-login/reauthentication)

我尝试使用FacebookAuthenticationOptions,但没有成功。

app.UseFacebookAuthentication(
           appId: "APP-ID",
           appSecret: "SECRET");

var options = new FacebookAuthenticationOptions();
// Details ommitted for simplicity
app.UseFacebookAuthentication(options);

我想另一种选择是在 ASP.NET 中使用 Facebook 的 Javascript SDK,但我认为这种 Facebook 重新身份验证应该可以通过 ASP.NET 的FacebookAuthenticationOptions 类开箱即用?

也许我错过了什么......

【问题讨论】:

    标签: c# asp.net asp.net-mvc facebook facebook-graph-api


    【解决方案1】:

    看起来我们需要构建一个自定义函数来添加额外的参数 - auth_type=reauthenticate 需要 facebook 重新认证。请参考Force re-authentication using OAuthWebSecurity with Facebook。引用关键代码部分供您参考

     private const string AuthorizationEP = "https://www.facebook.com/dialog/oauth";
            private const string TokenEP = "https://graph.facebook.com/oauth/access_token";
            private readonly string _appId;
            private readonly string _appSecret;
    
            public MyFacebookClient(string appId, string appSecret)
                : base("facebook")
            {
                this._appId = appId;
                this._appSecret = appSecret;
            }
    
    
            protected override Uri GetServiceLoginUrl(Uri returnUrl)
            {
                return new Uri(
                            AuthorizationEP
                            + "?client_id=" + this._appId
                            + "&redirect_uri=" + HttpUtility.UrlEncode(returnUrl.ToString())
                            + "&scope=email,user_about_me"
                            + "&display=page"
                            + "&auth_type=reauthenticate"
                        );
            }
    
            protected override IDictionary<string, string> GetUserData(string accessToken)
            {
                WebClient client = new WebClient();
                string content = client.DownloadString(
                    "https://graph.facebook.com/me?access_token=" + accessToken
                );
                dynamic data = Json.Decode(content);
                return new Dictionary<string, string> {
                    {
                        "id",
                        data.id
                    },
                    {
                        "name",
                        data.name
                    },
                    {
                        "photo",
                        "https://graph.facebook.com/" + data.id + "/picture"
                    },
                    {
                        "email",
                        data.email
                    }
                };
            }
    
            protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
            {
                WebClient client = new WebClient();
                string content = client.DownloadString(
                    TokenEP
                    + "?client_id=" + this._appId
                    + "&client_secret=" + this._appSecret
                    + "&redirect_uri=" + HttpUtility.UrlEncode(returnUrl.ToString())
                    + "&code=" + authorizationCode
                );
    
                NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(content);
                if (nameValueCollection != null)
                {
                    string result = nameValueCollection["access_token"];
                    return result;
                }
                return null;
            }
    

    【讨论】:

      【解决方案2】:

      您可以覆盖 FacebookAuthenticationProvider

      完整的解决方案在这里

      https://stackoverflow.com/a/41975557/341326

      【讨论】:

        猜你喜欢
        • 2015-04-16
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 2011-02-28
        • 1970-01-01
        • 2015-09-17
        相关资源
        最近更新 更多