I'm trying to setup integrated OWIN Facebook authentication in a new MVC 5 project in Visual Studio 2013. I have configured apps and keys as per this tutorial:
However, I'm getting a NullReferenceException thrown from this call in the AccountController:
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
I already checked the response in Fiddler and am getting what appears to be a success response from Facebook, but still get this error. The response looks like this:
{"id":"xxx","name":"xxx","first_name":"xxx","last_name":"xxx","link":
"https:\/\/www.facebook.com\/profile.php?id=xxx","location":{"id":"xxx","name":"xxx"},
"gender":"xxx","timezone":1,"locale":"en_GB","verified":true,"updated_time":"2013-10-23T10:42:23+0000"}
I get this when debugging in http as well as https. I'm guessing this is a framework bug but have so far drawn a blank diagnosing this through reflector.
I started getting this in the latest VS 2013.3 template and realized the authentication wasn't playing nice with FormsAuthentication that I unnecessarily ported from one of my other projects. Here's what I did to fix it:
added
<system.web><authentication mode="None" />...
added
<system.webServer><modules><remove name="FormsAuthentication" /></modules>...
I faced the same problem, when I checked libraries, I was using Microsoft ASP.NET Identity Owin 1.0.0 I updated it to Microsoft ASP.NET Identity Owin 2.0.1 using command PM> Install-Package Microsoft.AspNet.Identity.Owin -Version 2.0.1 This fixed the issue.
I came across this post a few days ago but unfortunately none of the above solutions worked for me. so here is how I managed to fix it and get the email from Facebook.
Microsoft.Owin
to version3.1.0-rc1
Microsoft.Owin.Security
to version3.1.0-rc1
Microsoft.Owin.Security.Cookies
to version3.1.0-rc1
Microsoft.Owin.Security.OAuth
to version3.1.0-rc1
Microsoft.Owin.Security.Facebook
to version3.1.0-rc1
Then add the following code to the
Identity Startup
classThis is the definition class for
FacebookBackChannelHandler()
:This probably is a bug in identity OWIN extension code. I can't repro the issue as my facebook payload always returns a username field in json, which is missing from your fb response. I am not quite sure why it's not there.
The code in identity owin extension method doesn't have a null check for the identity's name claim which is same as the username field. We have filed a bug for it internally.
In order to workaround this issue, could you try replacing your ExternalLoginCallback method with following code:
The code will set default user name as empty when there is no username back from facebook/google.
I was getting the same.
I noticed that my providers were configured before
UseExternalSignInCookie
was called, so I simply made sureUseExternalSignInCookie
is called before my providers are configured and everything worked: