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:
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and role manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(
// clientId: "",
// clientSecret: "");
}
}
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.