在ASP.NET MVC 5中,Session cookie是默认启用的,但很多情况下我们并不需要它。如果想关闭自动添加Session cookie的功能,可以在Global.asax文件中添加如下代码:
protected void Application_Start()
{
//关闭Session
SessionStateSection sessionStateSection = (SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
sessionStateSection.Mode = SessionStateMode.Off;
}
这样,就可以关闭Session并避免自动添加cookie到响应中。当然,如果需要使用Session,请将SessionStateMode设置为其他值,比如InProc、SQLServer等。
另外,如果只是想删除已经存在的Session cookie,可以在响应头中添加如下代码:
Response.Cookies.Remove("ASP.NET_SessionId");