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

前端 未结 16 1351
孤城傲影
孤城傲影 2020-12-25 10:01

I have a rather strange issue i\'m not sure how to fix or if i can even fix it.

I\'ve done some research into the issue but can\'t find an answer to what\'s causing

相关标签:
16条回答
  • 2020-12-25 10:20

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

    It seems your cookie is invalid.

    0 讨论(0)
  • 2020-12-25 10:20

    Clearing OWIN cookie worked in my case.

    0 讨论(0)
  • 2020-12-25 10:21

    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.

    0 讨论(0)
  • 2020-12-25 10:27

    The reason also could be in the differences of .Net Framework versions for compiling and running the application. Just explicitly specify the version in web.config:

    <httpRuntime ...... targetFramework="4.6.1" />
    <compilation ...... targetFramework="4.6.1" />
    
    0 讨论(0)
  • 2020-12-25 10:34

    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.

    0 讨论(0)
  • 2020-12-25 10:35

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

    0 讨论(0)
提交回复
热议问题