Im getting an null reference exception sometimes when I login with facebook using the out of the box ASP.NET mvc5 accounts controller.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 232: public async Task ExternalLoginCallback(string returnUrl) Line 233: { Line 234: var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); Line 235: if (loginInfo == null) Line 236: {
Hi,Previously i have the same error.I resolve it through
Tools->Library Package Manager->Manages Nuget Packages for Solutions->Microsoft.owin.security.Facebook
Just add that referance you will get no error now
Clearing the Session as per the reply from Lee (marked as the answer) resolved this issue for us too. We have a pretty stock standard ASP.NET MVC 4 web app with Google and Facebook login also enabled running on Azure Websites and this was driving us nuts.
It would stop working every 12 - 24 hours or somewhere around that time period and restarting the website would make it work for the next period until it happened again.
I do however wonder why clearing the session works... it smells a bit like a framework bug (or perhaps an Azure bug in our case) unless I'm missing something.
A quick solution for now.
You have to clear the Session before ExternalLoginCallback. Example.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
ControllerContext.HttpContext.Session.RemoveAll();
// Request a redirect to the external login provider
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
Upgrading my Owin components from version 3.0.1 to version 3.1.0 fixed this (so far). 3.1.0 was released April 10, 2017.
My similar issue was with Google, I haven't tackled FB yet. It worked fine locally, but crapped out when I published to a server.
In addition to what Lee mentioned with adding: ControllerContext.HttpContext.Session.RemoveAll();
I also applied: Best way in asp.net to force https for an entire site?
I also added to the Startup.Auth.cs file:
var gProvider = new GoogleAuthenticationProvider { OnAuthenticated = context => Task.FromResult(0) };
var gOptions = new GoogleAuthenticationOptions { Provider = gProvider, SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie, AuthenticationMode = AuthenticationMode.Active };
app.UseGoogleAuthentication(gOptions);
After I deployed my code, I also restarted IIS, the app pool, and the website. While I realize restarting IIS after a deployment is not ideal, this was the only site on the machine so I didn't have to worry about taking down other site.
I had a facebook account that did not have a user name set that was giving me a null exception message on that line of code. It seems the problem here is that some properties are expected in the account when returning information back to your application, and if those properties aren't present, it bombs. In my case, setting the user name in facebook fixed the problem.