Setup:
I have an MVC 5 app with a number of library projects, created using my own exported templates. The exported templates have been working fine.
I am using ASPNET Identity. I am just using a copy of the Microsoft Aspnet Identity Sample as supplied in the relevant NuGet package, that I have woven into my exported templates. This has been working fine.
I have not touched the files supplied in the ASPNET Identity 2 sample.
The error happens in the IdentityConfig.cs file.
For some reason, it started coming up with an error stating that it could not load the file for System.Web.Mvc, as it could not find version 5.1.0.0.
As a result, I used NuGet to update the Microsoft.Aspnet.Mvc package. This installed version 5.2.2.0 of system.web.mvc, and this effectively cleared that error.
However...
Although the application loads, whenever I try to login or create a new user, a new error comes up (shown below), essentially stating that the ASPNET Identity UserManager object was null.
I updated the microsoft.aspnet.identity package, but the error still happens when trying to login or create a new user (the login page displays ok, but the error happens when you click the log in button)
Before getting the error regarding the system.web.mvc reference, I could log in and register users at my leisure.
Error:
This is the error shown when I try to login. When I try to register a new user, I get a different error, but with the same cause: the UserManager object is null, when it shouldn't be.
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 324: public async Task<SignInStatus> PasswordSignIn(string userName, string password, bool isPersistent, bool shouldLockout)
Line 325: {
Line 326: var user = await UserManager.FindByNameAsync(userName);
Line 327: if (user == null)
Line 328: {
Source File: c:\Users\[user name]\Documents\Visual Studio 2013\Projects\[My solution]\Models\IdentityConfig.cs Line: 326
Question:
Does anyone know what could be causing this?
Could, for example, it be possible that the Microsoft Aspnet Identity sample code needs updating for version 5.2.2.0 of the system.web.mvc dll?
NOTE: I'm afraid I can't determine or recall what I changed just before the errors started happening. I haven't been working on this project for a while.
I have, after much pain, found the answer:
For some reason, the Startup file (~/App_Startup/Startup.Auth.cs), that was supposed to hold code to configure owin, did not. Not sure how this happened.
So I copied the corresponding file from the Microsoft.Aspnet.Identity.Samples code and it now works. The code is:
I had been using the code previously, but did remove the owin packages at one point. I did not touch the file manually, so still not sure how this happened.