Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 it.

I'm following a rather 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.

Object reference not set to an instance of an object.

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

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

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

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an 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 task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +810 System.Runtime.CompilerServices.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.MoveNext() +427 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 Microsoft.AspNet.Identity.Owin.d__0.MoveNext() +641 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 Microsoft.AspNet.Identity.Owin.d__0.MoveNext() +641 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 Microsoft.AspNet.Identity.Owin.d__0.MoveNext() +641 System.Runtime.CompilerServices.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__5.MoveNext() +287 System.Runtime.CompilerServices.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__2.MoveNext() +272 System.Runtime.ExceptionServices.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(IExecutionStep step, Boolean& completedSynchronously) +155

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

Turning off SSL fixes the issue, I also know that commenting out the startup.auth in app_start fixes the issue on SSL.

Does anyone know why this is happening?

like image 929
micah hawman Avatar asked Oct 12 '14 16:10

micah hawman


People also ask

What is null exception in C#?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.

What happens when you throw an exception that is actually null?

That’s the same thing that happens here. When the exception throwing mechanism is activated, if the exception given to it is actually null, then it will throw a NullReferenceException at that site instead. And this exception is not special, but just a plain old NullReferenceException .

What is a nullreferenceexception in Java?

When the exception throwing mechanism is activated, if the exception given to it is actually null, then it will throw a NullReferenceException at that site instead. And this exception is not special, but just a plain old NullReferenceException .

Is it a good idea to return Null from a service?

Returning null there is a good idea because the client code can know that something errored out in your web service. In the case of the client, I think the way you have it is good. I don't think there is a reason to throw another exception (even though you aren't in the web service anymore).

How does the exception throwing mechanism work in Java?

When the exception throwing mechanism is activated, if the exception given to it is actually null, then it will throw a NullReferenceException at that site instead. And this exception is not special, but just a plain old NullReferenceException . If you have a catch (NullReferenceException), it will catch it. Same with a catch (Exception) .


1 Answers

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.

like image 51
Ben Cull Avatar answered Oct 14 '22 14:10

Ben Cull