SignInManager.PasswordSignInAsync Throwing Null Object Exception

  • 6 Views
  • Last Post 10 September 2019
admin posted this 10 September 2019

 

AccountController.cs houses the AccountController class. The Login method of that class is failing. Specifically,

var result = await SignInManager.PasswordSignInAsync(

    model.Email, model.Password, model.RememberMe, shouldLockout: true);

within the Login method is throwing System.NullReferenceException.

I've verified that model.Email, model.Password, and model.RememberMe are not null. The next step is to dig in to SignInManager, which is an object on AccountController of type ApplicationSignInManager. The get accessor for SignInManager, which is called from the line above, was

public ApplicationSignInManager SignInManager

{

    get

    {

        return _signInManager ??

            HttpContext.GetOwinContext().Get<ApplicationSignInManager>();

    }

    // private set...

}

which I rewrote as

public ApplicationSignInManager SignInManager

{

    get

    {

        var c = HttpContext.GetOwinContext();

        var m = c.Get<ApplicationSignInManager>();

        return _signInManager ?? m;

    }

    // private set...

}

to debug and make sure that the get accessor was not returning null. It's not returning null. This tells me that something required inside PasswordSignInAsync is null.

I've looked at the source of PasswordSignInAsync and it does a pretty good job of guarding against null reference errors. I've also dug deeper into the methods called by PasswordSignInAsync. I simply don't see what could be throwing a null reference error.

 

 

 

admin posted this 10 September 2019

The error message indicates that EF Core is trying to read string value for a required property, i.e. a property which should never has null value in the database, but instead the underlying data reader reports null value for that property in some record{s).

 

You need to fix that discrepancy - probably by removing [Required] attribute because the constraint is already broken in the existing data.

The fact that it "works" in some older EF Core version doesn't matter - that's incorrect mapping and as such should be fixed. Technically it shouldn't worked from the beginning. But remember that EF Core is still in active development and has many bugs which are fixed in the next release(s). Most likely some code change was made between "working" and "non working" EF Core version which fixes the previous incorrect behavior.

Close