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

允我心安 提交于 2019-11-30 01:11:58

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.

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 => { }
    }
});

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.

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.

I had this issue too and solved it by clearing cookies.

It seems your cookie is invalid.

The reason this exception is probably being thrown is because there is a problem creating your ApplicationDbContext.

In my case I added Migrations, and set

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());

And I started getting the same error as you.

Turned out that when I tried to access any object in the database, the DbContext was throwing an error, saying AspNetUsers already exists as previously I have run my code without migrations enabled, therefore the Database was created, with all the correct tables needed for Identity, and once I did EnableMigrations, and set an initialiser, it was throwing an error saying that the Table already exists.

In your case, my guess would be there is some underlying issue with the ApplicationDbContext, before the Startup, try the following before Config.Auth methods get called:

        var ctx = new ApplicationDbContext(...);
        var count = ctx.Users.Count();

See if it returns the count or throws an exception.

Yes i had the same problem, i downloaded a database from Azure. I then change my app to use this My app had a new field that was not present in the azure backup Migrations were out of sync.

Update-Database (in package manage with migrations enabled) did the trick.

Update-Database in Package Manager Console did the trick for me

These answers all seem helpful and to indicate a trend that the database is messed up. I got this issue because I had updated my model and hadn't updated the database. Right now I'm calling Add-Migration and Update-Database manually each time I change the model and before I try to debug my site and I had forgotten that step.

Omid-RH

clear localhost cookies. if use firefox see this link. I have same error exactly and this solution here.

I had the same problem and it happened because the SQL database is behind a firewall and you need to add your local IP every time it changes.

See this link from Microsoft for all the ways to do that:

http://msdn.microsoft.com/en-us/library/azure/jj553530.aspx

Try to remove the Migration from the project

I happened with me once I've enabled the database migration for the Identity DB

after remove the entire migration folder and rebuild the problem disappeared

it might work for you

Clearing OWIN cookie worked in my case.

After reading some answers, trying in IE instead of Chrome and seeing no crash, I just closed Chrome and restarted the app. It worked.

i missed to add the roles in the table [AspNetRoles]. That solved the issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!