NovumCoder
NovumCoder

Reputation: 4667

ASP.NET WebForms with API without Cookies

I have a ASP.NET WebForms application which also has API code. This project is not MVC based, its just WebForms.

The API is a seperate class with WebMethods, which is being routed to via the following code in Application_Start (file Global.asax.cs)

protected void Application_Start(object sender, EventArgs e)
{
        log4net.Config.XmlConfigurator.Configure();
        RouteTable.Routes.Add(new ServiceRoute("api/v1", new WebServiceHostFactory(), typeof(API.v1.Api)));
}

Everything works fine, once a request comes in to domain.com/api/v1/... it will be routed to my Api class. But my application is always responding with the "Set-Cookie" Header even when on the Api route. I'm not using the Session context anywhere in the Api class.

How can i disable Cookie entirely for everything within /api while at the root of the application cookie still runs as usual? Is that even possible?

Upvotes: 0

Views: 35

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

Add this to your global.asax file. It will be auto-bound.

Remove any other cookies as well, if necessary. This is tested in an environment equal to yours and prooved to be working.

protected void Application_PreSendRequestHeaders(Object source, EventArgs e)
{
    if (Request.Url.AbsolutePath.ToLowerInvariant().Contains("/api/v1"))
    {
        Response.Cookies.Remove("ASP.NET_SessionId");
    }
}

Upvotes: 1

Related Questions