1

I have to set a cookie path attribute in the asp.net application. I am getting "" only path if I changed the session state .Please help me to how to change cookie path in the asp.net application

    //web config

//global asax protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) { // only apply session cookie persistence to requests requiring session information if (Context.Handleris IRequiresSessionState || Context.Handleris IReadOnlySessionState) { var sessionState = ConfigurationManager.GetSection("system.web/sessionState")as SessionStateSection; var cookieName = sessionState !=null && !string.IsNullOrEmpty(sessionState.CookieName) ? sessionState.CookieName :"ASP.NET_SessionId";

    var timeout = sessionState !=null ? sessionState.Timeout : TimeSpan.FromMinutes(20);

    // Ensure ASP.NET Session Cookies are accessible throughout the subdomains.
    if (Request.Cookies[cookieName] !=null && Session !=null && Session.SessionID !=null)
    {
        Response.Cookies[cookieName].Value = Session.SessionID;
        Response.Cookies[cookieName].Path = Request.ApplicationPath;
        Response.Cookies[cookieName].Expires = DateTime.Now.Add(timeout);
    }
}

}

1 Answers1

0

you should Provide Default Path for your Cookie which is Server root. the Below Code snippet shows how to set Default Server Root path

 HttpCookie NewCookie = new HttpCookie("ASP.Net_SessionId");
 NewCookie.Expires = DateTime.Now.AddYears(1);
 NewCookie.Secure = false;
 NewCookie.Path = "/";
 NewCookie.Domain = "DomainName";
 HttpContext.Current.Response.Cookies.Add(NewCookie);

so basically as your Code, you can do Following

Response.Cookies[CookieName].Path = "/"; 
Vinay Soni
  • 88
  • 1
  • 6