0

I need to delete a specific cookie when my app starts, before heading to home page.

I had this inside a controller action method, with a redirect to home page, setting up my startup class to use as route template this controller and action method.

However, there must be a way I can set up a method to delete this cookie, and execute it from startup?

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
  • 1
    Nope, the cookie is in the browser, your startup method can't do anything about that. You either replace the cookie from an endpoint, or via javascript in the view that is loaded. Or you construct your cookies so you can treat all prior cookies as invalid. – Jeremy Lakeman Jan 05 '20 at 10:14

1 Answers1

1

In ASP.NET, this would be done in the methods of global.asax (often in Session_Start(...)). Read more here and here.

In ASP.NET Core, the startup.cs class is where all configurations of services are defined, as well as pipeline requests are managed.

You need to make your own custom middleware for this. Middleware is software that's assembled into an app pipeline to handle requests and responses.

There is another SO question on this topic here (with an answer):

ASP .NET Core webapi set cookie in middleware

For more in-depth cookie management look at this article:

https://www.seeleycoder.com/blog/cookie-management-asp-net-core/

More on middleware:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-2.2

Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24