2017 update!
The problem that I encountered when I posted the original question has nothing to do with the recent changes made by Facebook when they forced everyone to version 2.3 of their API. To solve this particular problem, see sammy34 answers below . Version 2.3 of the endpoint / oauth / access _token now returns JSON instead of formatted values
For historical reasons, here is my original question / question:
I have an MVC5 web application that uses native authentication support through Facebook and Google. When we built this application a few months ago, we followed this guide: http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and- google-oauth2-and-openid-sign-on and everything worked fine.
Now, all of a sudden, Facebook authentication just stopped working all together. Google Authentication still works great.
Description of the problem: we click on the link to connect using Facebook, we are redirected to Facebook, where we will be asked if we do not allow our Facebook application to access our profile. When we click "OK", we are redirected back to our website, but instead of logging in, we simply find ourselves on the login screen.
I went through this process in debug mode, and I have this ActionResult in my account controller according to the guide mentioned above:
// GET: /Account/ExternalLoginCallback [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return RedirectToAction("Login"); } ............
When navigating through the code and returning from Facebook, the loginInfo object is always NULL, which forces the user to be redirected back to the input.
To understand what is really going on behind the scenes, I installed Fiddler and monitored HTTP traffic. What I turned off is that after clicking “OK” in the Facebook permission dialog, Facebook redirects back to our application with this URL:
https:
This url is not the actual file and is probably being handled by some kind of controller / handler built into this OWIN structure, I guess. Most likely, he connects to Facebook using the specified code to request information about a user who is trying to log in. Now the problem is that instead we are redirected to:
/Account/ExternalLoginCallback?error=access_denied
I am sure that this is what Facebook does, that is, instead of giving us user data, it redirects us with this error message.
This causes the AuthenticationManager.GetExternalLoginInfoAsync(); to fail AuthenticationManager.GetExternalLoginInfoAsync(); and always returns NULL.
I have a lot of ideas. As far as we know, we have not changed anything on our part.
I tried to create a new application for Facebook, I tried it again, but I always had the same problem.
Any ideas are welcome!
Update
Ok, this is driving me crazy! Now I manually completed the steps necessary for authentication, and everything works fine when I do this. Why doesn't this work when using MVC5 Owin materials?
This is what I did:
// Step 1 - Pasted this into a browser, this returns a code https://www.facebook.com/dialog/oauth?response_type=code&client_id=619359858118523&redirect_uri=https%3A%2F%2Flocalhost%2Fsignin-facebook&scope=&state=u9R1m4iRI6Td4yACEgO99ETQw9NAos06bZWilJxJrXRn1rh4KEQhfuEVAq52UPnUif-lEHgayyWrsrdlW6t3ghLD8iFGX5S2iUBHotyTqCCQ9lx2Nl091pHPIw1N0JV23sc4wYfOs2YU5smyw9MGhcEuinvTAEql2QhBowR62FfU6PY4lA6m8pD3odI5MwBYOMor3eMLu2qnpEk0GekbtTVWgQnKnH6t1UcC6KcNXYY I was redirected back to localhost (which I had shut down at this point to avoid being redirected immediately away). The URL I was redirected to is this: https://localhost/signin-facebook?code=<code-received-removed-for-obvious-reasons> Now, I grabbed the code I got and used it in the URL below: // Step 2 - opened this URL in a browser, and successfully retrieved an access token https://graph.facebook.com/oauth/access_token?client_id=619359858118523&redirect_uri=https://localhost/signin-facebook&client_secret=<client-secret>&code=<code-from-step-1> // Step 3 - Now I'm able to query the facebook graph using the access token from step 2! https://graph.facebook.com/me?access_token=<access-token-from-step-2>
No mistakes, everything works great! Then why the hell doesn’t it work when using the MVC5 Owin material? There is clearly something wrong with the OWin implementation.