C# Owin cookie身份验证偶尔会引发NullReferenceException

C# Owin cookie身份验证偶尔会引发NullReferenceException,c#,asp.net-mvc,authentication,cookies,owin,C#,Asp.net Mvc,Authentication,Cookies,Owin,我正在一个项目中使用OWIN cookie身份验证中间件,该项目被设置为ASP.NET MVC和WebApi应用程序(即,我添加了OWIN) 时不时地,当我做一些更改并开始调试时,我会遇到一个异常,每次请求都会出现一分钟左右,直到网站突然恢复正常工作,不再出现任何问题。我在本地IIS中托管应用程序 System.NullReferenceException: Object reference not set to an instance of an object. at FooWeb.St

我正在一个项目中使用OWIN cookie身份验证中间件,该项目被设置为ASP.NET MVC和WebApi应用程序(即,我添加了OWIN)

时不时地,当我做一些更改并开始调试时,我会遇到一个异常,每次请求都会出现一分钟左右,直到网站突然恢复正常工作,不再出现任何问题。我在本地IIS中托管应用程序

System.NullReferenceException: Object reference not set to an instance of an object.
   at FooWeb.Startup.<>c.<Configuration>b__0_3(CookieExceptionContext context) in C:\ws\Foo\Main\Main\FooWeb\Startup.cs:line 138
   at Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler.<ApplyResponseGrantAsync>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<ApplyResponseCoreAsync>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<ApplyResponseAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<TeardownAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.<Invoke>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.<RunApp>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.<DoFinalWork>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
   at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously);

更新这似乎与cookie或至少与浏览器相关-因为我在浏览器中有一个会话,它将始终抛出该异常,而其他浏览器(以前也登录过)可以访问该异常。

我们遇到了相同的问题。两件事:

  • 确保您使用的至少是1.15.0版本的 Owin.Security.Providers
  • 使用

  • 您可能会发现以下帖子很有用:

    与其拒绝身份,不如用匿名身份替换它

    context.ReplaceIdentity(System.Security.Principal.WindowsIdentity.GetAnonymous());
    

    我没有看到任何代码可以访问可为null的对象或对象的属性。这个异常似乎不是源于我自己的代码。此外,即使我这样做了,调试也会在特定的异常处停止。尝试了这两种方法,但都没有帮助
    context.ReplaceIdentity(System.Security.Principal.WindowsIdentity.GetAnonymous());