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.
The stack trace from the error is pretty cryptic, and I don't see anything in it that helps. But for the sake of completeness, here it is:
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +2112
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123
Microsoft.AspNet.Identity.Owin.<SignInAsync>d__2.MoveNext() +408
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
Microsoft.AspNet.Identity.CultureAwaiter.GetResult() +63
Microsoft.AspNet.Identity.Owin.<SignInOrTwoFactor>d__23.MoveNext() +2055
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +68
Microsoft.AspNet.Identity.Owin.<PasswordSignInAsync>d__29.MoveNext() +1828
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
ApexRemingtonExternal.Controllers.<Login>d__2.MoveNext() in c:\Users\me\sc\project\Controllers\AccountController.cs:91
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +66
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651688
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
I'd appreciate any help or guidance on what could be causing this NullReferenceException
. Please let me know if I can add more detail (or remove some (lol!)). This is my and my company's first MVC project, so I'm sure I've broken something.
Edit
I should add that I stripped out Entity Framework from the project in favor of LINQ to SQL, which is what we rely on in all of our other projects. That means that I've had to implement my own versions of IUserStore, IRoleStore, etc.
A little late in the game, but I encountered this error today and found out that the cause of the null reference exception is the null property within ApplicationUser.
This happens when you create custom claims within your Applicationuser.
When you call SignInManager.PasswordSignInAsync
it will GenerateUserIdentity and that will include your custom claims. Any null objects in there will throw NULL REFERENCE EXCEPTION.
To show some sample code (please see comments):
public class ApplicationUser : IdentityUser
{
...//some additional properties goes here
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
//my custom claims
List<Claim> claims = new List<Claim>
{
new Claim("FirstName", FirstName.ToString()),
new Claim("MiddleName", MiddleName.ToString()),
new Claim("LastName", LastName.ToString()),
new Claim("Position", Position.ToString()) //if Position is NULL, it will throw null reference exception
};
userIdentity.AddClaims(claims);
return userIdentity;
}
}
I hope this will help somebody in the future.
Sometimes (especially if you update Microsoft.AspNet.Identity.Owin
) you can forgot to add line
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
to method public void ConfigureAuth(IAppBuilder app)
in App_Start\Startup.Auth.cs
After including this everything should work.
As is usually my wont, the issue had nothing to do with the symptom. Of course SignInManager.PasswordSignInAsync
isn't broken. It was my code that was broken. That makes this question moot, which begs me to delete the question.
There is, however, a lesson to be learned here. In my case it was the as
keyword. Having abandoned EF, as mentioned in the edit, I had to supply implementations of several interfaces. One method I had to supply is GetRolesAsync
, which returns Task<IList<String>>
(in my case). I've never dealt with IList
. Turns out as
is what I needed. But I was trying to cast an IQueryable
to an IList
, which doesn't work. The result: as
simply returns null
. Thanks, as
. That's useful.
Yeah. I'll probably delete this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With