FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri()) 在 Facebook 上返回 null [英] FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri()) return null on Facebook

查看:14
本文介绍了FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri()) 在 Facebook 上返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 nopcommerce 开发了一个 mvc 5 应用程序,我使用外部回调使用 facebook 登录,它正在工作,但现在它不起作用,我找不到实际问题.并使用下面的代码

I've developed an mvc 5 application using nopcommerce and i use facebook login using External callback it was working but now it is not working and i can't find out actual problem. And using this below code

this.FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri());

它总是返回 null 并且身份验证状态失败我在网上搜索并执行所有操作并按照这些步骤操作,但我仍然无法使用 Facebook 登录.

and it's returning me always null and authentication status failed i searched on web an do every thing and followed that steps but still i can't login with facebook.

我在FacebookProviderAuthorizer.cs中的代码是这样的

My code is like this in FacebookProviderAuthorizer.cs

private AuthorizeState VerifyAuthentication(string returnUrl)
{
   var authResult = DotNetOpenAuth.AspNet.Clients.FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri());

   if (authResult.IsSuccessful)
   {
   }
}

然后写回调方法

private Uri GenerateLocalCallbackUri()
{
    string url = string.Format("{0}plugins/externalauthFacebook/logincallback/", _webHelper.GetStoreLocation());
    return new Uri(url);            
}

然后生成服务登录url

Then generate service login url

private Uri GenerateServiceLoginUrl()
{
   //code copied from DotNetOpenAuth.AspNet.Clients.FacebookClient file
   var builder = new UriBuilder("https://www.facebook.com/dialog/oauth");
   var args = new Dictionary<string, string>();
   args.Add("client_id", _facebookExternalAuthSettings.ClientKeyIdentifier);
   args.Add("redirect_uri", GenerateLocalCallbackUri().AbsoluteUri);
   args.Add("response_type", "token");
   args.Add("scope", "email");
   AppendQueryArgs(builder, args);
   return builder.Uri;
}

推荐答案

我们在 2017 年 3 月 27 日星期一遇到了同样的问题,当时 Facebook 停止了对其 Graph API v2.2 的支持.

We ran into this same issue on Monday, 3/27/2017, when Facebook discontinued support for their Graph API v2.2.

我们还在使用最初通过 Nuget 安装的 DotNetOpenAuth.源代码可从以下链接获得:

We are also using DotNetOpenAuth, which was originally installed via Nuget. The source code is available at the link below:

https://github.com/DotNetOpenAuth/DotNetOpenAuth

特别是,我们发现我们的代码使用了 4.3 分支,其中包含 DotNetOpenAuth.AspNet.DLL 的源代码.在检查源代码后,我们发现问题出在 DotNetOpenAuth.AspNetClientsOAuth2FacebookClient.cs 中的这段代码中,位于 QueryAccessToken 方法中:

Specifically, we discovered that our code was utilizing the 4.3 branch which contains the source code for the DotNetOpenAuth.AspNet.DLL. Upon inspecting the source, we discovered the problem is with this snippet of code from DotNetOpenAuth.AspNetClientsOAuth2FacebookClient.cs, located within the QueryAccessToken method:

using (WebClient client = new WebClient()) {
     string data = client.DownloadString(builder.Uri);
     if (string.IsNullOrEmpty(data)) {
          return null;
     }

     var parsedQueryString = HttpUtility.ParseQueryString(data);
     return parsedQueryString["access_token"];
}

问题特别是 ParseQueryString 调用.从 API 的 v2.3 开始,数据不再作为 HTML 查询字符串返回,而是以标准 JSON 格式返回.

The issue, specifically, is the ParseQueryString call. Starting with v2.3 of the API, the data is no longer returned as an HTML query string, but in standard JSON format.

为了解决这个问题,我们创建了自己的自定义类,继承了 OAuth2Client,并从 FacebookClient.cs 导入了大部分相同的代码.然后,我们将上面的代码片段替换为解析 JSON 响应以提取 access_token 并返回的代码.您可以在同一 FacebookClient 类的 GetUserData 方法中查看如何执行此操作的示例:

To fix this, we created our own custom class inheriting OAuth2Client and imported most of the same code from FacebookClient.cs. We then replaced the above code snippet with code that parses the JSON response to extract the access_token, and returns that instead. You can see an example of how to do this in the same FacebookClient class, within the GetUserData method:

FacebookGraphData graphData;
var request =
    WebRequest.Create(
        "https://graph.facebook.com/me?access_token=" +
             MessagingUtilities.EscapeUriDataStringRfc3986(accessToken));
using (var response = request.GetResponse()) {
     using (var responseStream = response.GetResponseStream()) {
        graphData = JsonHelper.Deserialize<FacebookGraphData>(responseStream);
     }
}

唯一的其他更改是注册我们的自定义类来代替 FacebookClient 类,以便 OAuth 回调使用它来处理来自 Facebook API 的帖子.一旦我们这样做了,一切又会很顺利.

The only other change was to register our custom class in place of the FacebookClient class so the OAuth callback uses it to handle the post from Facebook's API. Once we did this, everything worked smoothly again.

这篇关于FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri()) 在 Facebook 上返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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