Why is [Owin] throwing a null exception on new project?

Solution 1:

Similar to Sandeep's answer, I also updated the cookie authentication provider. Except, instead of swallowing the error I threw the exception so you could see what the underlying problem was:

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, User>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),

            /* I changed this part */
            OnException = (context =>
            {
                throw context.Exception;
            })
    }                
});

The underlying problem for me was that I had changed the model and forgotten to add a new migration.

Solution 2:

I was getting similar error but when I changed EF configuration from DropCreateDatabaseIfModelChanges< Context> to DropCreateDatabaseAlways< Context>.

I'm not sure about cause of your error but it seems like an issue in Katana Project https://katanaproject.codeplex.com/workitem/346

You can try workaround at above link or from https://katanaproject.codeplex.com/discussions/565294

This is what I did in my StartUp.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
           OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager, User>(
            validateInterval: TimeSpan.FromMinutes(1),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),

           //**** This what I did ***//
            OnException = context => { }
    }
});

Solution 3:

Running the following updates after creating a new project worked for me:

update-package Microsoft.Owin

update-package Microsoft.Owin.Security

update-package Microsoft.Owin.Security.Cookies

I think the last one might have been enough. I am using Visual Studio 2013 (12.0.21005.1) and the ASP.Net Web Application template with Web API.

Solution 4:

I agree that adding "OnException = context => {}" solves exception being displayed, but the next error I saw just now may suggest a common cause, and hence a first step to try before this fix.

I now have an error informing me that the model backing by context has changed. This may mean that attempting Add-Migration and Update-Database may resolve this for other ASP.NET Identity users who encounter this, and if that fails then add the line above. This would also suggest some of the basic checks like "Can I connect to the database?" may also be worth checking if you see this Owin Security exception. Once this subsequenct error was fixed I could happily remove the OnException line and the site is still working fine.