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

I have a rather strange problem, I'm not sure how to fix it or even can fix it.

I did some research on the problem, but I can not find the answer to the cause of this.

I follow a fairly simple guide at http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid- sign-on

and after enabling SSL and changing the controller to require https, I get the following error:

Server error in application "/".

The reference to the object is not installed in the object instance.

Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for error information and where it originated in the code.

Exception Details: System.NullReferenceException: object reference not installed on the object instance.

Source Error:

An unhandled exception was thrown during the execution of the current web request. Information about the origin and location of the exception can be identified using the exception stack trace below.

Stack trace:

[NullReferenceException: object reference not set to instance object.]
Microsoft.Owin.Security.Cookies.CookieAuthenticationProvider.Exception (CookieExceptionContext context) +49 Microsoft.Owin.Security.Cookies.d__2.MoveNext () +3698 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task) +52 .TaskAwaiter.ThrowForNonSuccess (Task Task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult () +21 Microsoft.Owin.Security.Infrastructure.d__0.MoveNextRate.aimer .ThrowForNonSuccess (Task Task) +93
System.Runtime .ThrowForNonSuccess (Task Task) +93
System.Runtime .ThrowForNonSuccess (Task Task) +93
System.Runtime .ThrowForNonSuccess (Task Task) +93
System.Runtime .TaskAwaiter.ThrowForNonSuccess (Task Task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult () +21 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2MoveNext.MoveNext.MoveNext.MoveNext.MoveNopeMentNemeTemeter.Integrated.ipegenter .ExceptionDispatchInfo.Throw () +22 Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow () +33 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End (IAsyncResult ar) +150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork (IAsyncResult ar) +42
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +415 System.Web.HttpApplication.ExecuteStep (step IExecutionStep, Boolean & completed synchronously) +155

Version Information: Microsoft.NET Framework Version: 4.0.30319; ASP.NET Version: 4.0.30319.34237

Disabling SSL fixes the problem, I also know that commenting on startup.auth in app_start fixes the problem on SSL .

Does anyone know why this is happening?

+47
c # nullreferenceexception owin
Oct 12 '14 at 16:04
source share
15 answers

As in Sandeep's answer, I also updated the cookie authentication provider. Also, instead of catching the error, I threw an exception so you can understand what the main problem is:

 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 main problem for me was that I changed the model and forgot to add a new migration.

+31
Feb 06 '15 at 17:32
source share

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

I'm not sure about the cause of your error, but this seems like a problem in the Katana project https://katanaproject.codeplex.com/workitem/346

You can try the workaround from the link above 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 => { } } }); 
+18
Nov 19 '14 at 11:47
source share

Performing the following updates after creating a new project for me:

Microsoft.Owin Service Pack

Microsoft.Owin.Security Service Pack

Microsoft.Owin.Security.Cookies Service Pack

I think the latter could be enough. I am using Visual Studio 2013 (12.0.21005.1) and the ASP.Net Web Application template with web API .

+9
Jul 15 '15 at 13:59 on
source share

I agree that adding "OnException = context => {}" allows the display of exceptions, but the next error that I saw now may indicate a common reason and, therefore, the first step to try before this fix.

Now I have an error message informing me that the model substitution by context has changed. This may mean that trying Add-Migration and Update-Database may resolve this for other ASP.NET Identity users who encounter this, and if that fails, add the line above. It would also suggest some basic checks, such as "Can I connect to the database?" It may also be worth checking to see if this is an Owin Security exception. Once this subsequence error has been fixed, I could happily delete the OnException line, and the site is still working fine.

+7
Dec 10 '14 at 18:18
source share

I had this problem too, and I decided to clear it by clearing the cookies.

Your cookie seems to be invalid.

+4
Jun 13 '15 at 9:11
source share

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

In my case, I added Migrations and installed

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

And I started to get the same error as you.

It turned out that when I tried to access any object in the database, DbContext threw an error saying that AspNetUsers already exist, since I had previously run my code without activating the migration, so a database was created with all the correct tables needed for Identity , and as soon as I did EnableMigrations and installed the initializer, it threw an error saying that the table already exists.

In your case, I assume that there are some basic problems with ApplicationDbContext, before starting, try the following methods before Config.Auth:

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

See if it returns an invoice or throws an exception.

+2
Jan 31 '15 at 19:48
source share

Yes, I had the same problem, I downloaded the database from Azure. Then I change my application to use this. A new field appeared in my application, which was absent in the azure backup. Migrations are not synchronized.

Updating the database (in managing packages with migrations enabled) did the trick.

+2
Feb 05 '15 at 21:58
source share

Updating the database in the package manager console did the trick for me

+2
Jul 09 '15 at 16:43
source share

These answers seem useful and indicate a trend that the database is confused. I got this problem because I updated my model and did not update the database. Right now, I call Add-Migration and Update-Database manually every time I change the model, and before I try to debug my site, and I forgot this step.

+1
May 10 '15 at 2:00
source share

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

+1
May 18 '15 at 18:32
source share

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

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

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

0
Dec 19 '14 at 2:36
source share

Try to remove Migration from the project

It happened to me as soon as I turned on database migration for the Identity database

after deleting the entire transfer folder and restoring the problem disappeared

it might work for you

0
Jan 13 '15 at 13:44
source share

Clearing the OWIN cookie worked in my case.

0
Mar 02 '15 at 21:01
source share

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

0
Mar 29 '15 at 14:46
source share

I skipped to add roles to the [AspNetRoles] table. This solved the problem.

0
May 7 '16 at 3:38
source share



All Articles