Im getting an null reference exception sometimes when I login with facebook using the out of the box ASP.NET mvc5 accounts controller.
Here is the dieing method :
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
// Crashes on this line
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
}
Im not sure how to debug this. A breakpoint and steeping though the code is no help… I end up looking at my Error.cshtml page. The error at that point is a simple object reference null exception and the inner exception is also null.
Edit
I updated to the latest Owins via Nuget, no change.
Edit 2
Took a look in fiddler, Facebook is returning a 200 with what looks like a correct profile as json.
Edit 3
So strange. Im testing with 3 facebook accounts. Two accounts are working fine, 1 does not. The failing one is returning with 200. I have removed the app references in facebook.. I get a app confirmation window, I click ok, and it dies…. so strange.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 }));
}
Method 2
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.
Method 3
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.
Method 4
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.
Method 5
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.
Method 6
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0