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;
}
Building onto Steve's post, I created a "FriendlyFacebookClient" to use in-place of FacebookClient, copied over some of the internal methods, and replaced QueryAccessToken with the following:
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:
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:
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.
I solve my issue I will do the same thing as @Adam described in his answer, As per the @Adam, @SteveTerry and @Adeem's answer i change my code and create custom FacebookClient class with different name. and replace with original reference of FacebookClient in nopCommerce.
in this code i have no reference of JsonHelper so i use simple jsonConvert. Thanx again @Adam, @SteveTerry and @Adeem's for help.
According to Previous Answer I got my solution. In
MVC4
everyone write down theirAppID
andSecurityCode
. Due to change of facebook GRAPH API those previous links are broken. Consequently everyone need to change theRegisterFacebookClient
class. But this class is a sealed class in the .Net library, so anyone can't extend or overwrite it. As a result we need to use awrapper
class. I am writing this answer because all the previous answer suppliers have missed one thing, for which I have suffered a lot. Because they did not mentioned the system of class warping system in theAuthConfig
Class. So it takes too much to solve this problem. Therefore I am going step by step as below. Let us consider my Wrapper class isFacebookClientV2Dot3
therefore my class will beLook here you I have replaces all the API links by newer version links.
Now you need to modify your
Just use a wrapper class instead of
RegisterFacebookClient
. Completely block those portion of code. And add this...Then all success. You facebook login will be back in previous state.
However you can face a new issue regarding this new API rather than previous API, the problem is that
IP Whitelisting
. Like this image. Hope you will need nothing but this. Happy coding.I have used the code shared by @Vishal and got the same working.
The main thing that we have to focus is to override the QueryAccessToken method to use json response.
Steps to achieve this: Step 1. What you have to do is add one file named FacebookClientOverride.cs(other than FacebookClient.cs)
Here is the code snippet of the whole file.
Step 2. Add one reference to System.Web.Extensions
Step 3. In the FacebookProviderAuthorizer.cs (Nopcommerce project) look for the FacebookClient Property private FacebookClient _facebookApplication;
This should refer to your new file just added.
Step 4. Now put a break point in the method named VerifyAuthentication in the file FacebookProviderAuthorizer.cs .
The authResult.IsSuccessful must be true now as it successfully parsed the token.
Thanks all. Please like if the soluutions worked for you.
As suggested by @SteveTerry We need to update QueryAccessToken function in FacebookClient class. unfortunately "FacebookClient" is sealed class so we cannot inherit and override. So whichever way you choose is up to you. Here is what end result should look like:
Old code of this function was:
}
And to support new version of fb api it should be like this: