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

问题描述

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



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



,它返回我一直为null,认证状态失败,我在网上搜索一个做每件事情,并按照这些步骤,但仍然我无法使用Facebook登录。



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

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

如果(authResult.IsSuccessful)
{
}
}

然后写回调方法

  private Uri GenerateLocalCallbackUri()
{
string url = string.Format({0} plugins / externalauthFacebook / logincallback /,_webHelper.GetStoreLocation());
返回新的Uri(url);
}

然后生成服务登录url

  private Uri GenerateServiceLoginUrl()
{
//从DotNetOpenAuth.AspNet.Clients.FacebookClient文件复制的代码
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;
}


解决方案

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



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





具体来说,我们发现我们的代码正在使用4.3分支,其中包含DotNetOpenAuth.AspNet的源代码.DLL。在检查源代码后,我们发现问题是来自DotNetOpenAuth.AspNet\Clients\OAuth2\FacebookClient.cs的代码片段,位于QueryAccessToken方法中:

  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格式返回。



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

  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的帖子。一旦我们这样做,一切都顺利地进行。


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());

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.

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)
   {
   }
}

And then write Call back method

private Uri GenerateLocalCallbackUri()
{
    string url = string.Format("{0}plugins/externalauthFacebook/logincallback/", _webHelper.GetStoreLocation());
    return new Uri(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;
}
解决方案

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

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

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.AspNet\Clients\OAuth2\FacebookClient.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"];
}

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.

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);
     }
}

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:26