我在一个设置为ASP.NET MVC和WebApi应用程序的项目中使用OWIN cookie身份验证中间件(即我添加了OWIN).
偶尔,当我做了一些更改并开始调试时,我得到一个异常,这个异常发生在每个请求上好一分钟左右,直到网站突然工作,没有任何问题.我在我的本地IIS中托管应用程序.
System.NullReferenceException: Object reference not set to an instance of an object. at FooWeb.Startup.<>c.b__0_3(CookieExceptionContext context) in C:\ws\Foo\Main\Main\FooWeb\Startup.cs:line 138 at Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler. 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. 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. 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. 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. 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. 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. 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);
我像这样设置中间件:
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", LoginPath = new PathString("/Account/Login"), LogoutPath = new PathString("/Account/Logoff"), CookieName = "FooWebCookieAuth", SlidingExpiration = true, ExpireTimeSpan = TimeSpan.FromMinutes(10), CookieSecure = CookieSecureOption.Always, Provider = new CookieAuthenticationProvider() { OnValidateIdentity = async context => { // Validate access token if (context == null) { return; } if (context.Identity == null || !context.Identity.IsAuthenticated) { return; } if (context.Identity.Claims == null) { context.RejectIdentity(); } var accessTokenClaim = context.Identity.Claims.FirstOrDefault(x => x.Type == FooClaimTypes.Token); var accessToken = (accessTokenClaim == null) ? null : accessTokenClaim.Value; if (accessToken == null) { context.RejectIdentity(); } else { var client = new IntrospectionClient( SecurityTokenServiceEndpoints.Introspection, "FooScope", "FooSecret"); var validationResult = await client.SendAsync(new IntrospectionRequest() { Token = accessToken }); if (validationResult.IsError || !validationResult.IsActive) { context.RejectIdentity(); } } }, OnException = context => { // exception is thrown here (so that debugging stops). Without this it just faults throw context.Exception; }, }, });
更新这似乎与cookie或至少与浏览器有关 - 因为我在浏览器中有一个会话,它会一直抛出该异常,而其他浏览器(以前也登录过)也可以正常访问它.