21

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.

1
  • 2
    Got this issue also. But every time I restarted IIS it fixes the issue.
    – Lee
    Jan 6, 2014 at 10:01

6 Answers 6

36

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 }));
}
10
  • This seems to work to fix the OWIN login problem where it stops working after about a day. I don't understand why it works, though. Aug 31, 2015 at 12:42
  • This works also for me, I think it's a nice fix. Anyway there seems to be a big bug in the asp.net mvc Identity system. Note: ControllerContext.HttpContext.Session.RemoveAll(); needs to be added also in ManageController->LinkLogin if you want to be able to associate an external account with your current account.
    – Pinte Dani
    Oct 6, 2015 at 18:50
  • Man, you're so brilliant figuring it out! Never expected the Session to be the problem! Been stuck at this OWIN bug for whole friggen day! Thanks a lot for sharing.
    – Hajjat
    Nov 16, 2015 at 1:02
  • The one and only solution that works for me :) Thanks Lee
    – Vlad
    Dec 19, 2015 at 16:22
  • Like all those above me, this fixed the issue. Absolutely no idea why.
    – user2415010
    May 6, 2016 at 15:55
6

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.

0
2

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.

1
  • I face same problem here with azure. It works max. 3 days and returns null for Azure AD auth. After cleaning session did it resolve for good ?
    – Cozdemir
    Feb 5, 2021 at 7:24
0

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.

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.