Suresh Raut
Suresh Raut

Reputation: 15

How to set cookie path in asp.net mvc

I am trying to set cookie path so that they should not be accessible to other applications on a shared server if path is set to root i.e. "/"

I am trying to set it as follows in web.config file:

<forms loginUrl="~/account/logon" timeout="2880" requireSSL="true" path="my_virtual_directory_name" />

I know this will only work for ".ASPXAUTH" cookie. In my case path is set to this cookie and another cookie with same name is created with path set as root.

I need to set path for all cookies and there should not be duplication of cookies, one with proper path and another with path set to root.

Please suggest me how can I set a fixed path for all cookies in asp.net mvc 4 application.

Thanks.

Upvotes: 2

Views: 12931

Answers (2)

Jeetendra Negi
Jeetendra Negi

Reputation: 453

use this is asp.net C#

 HttpCookie cookies=  HttpContext.Current.Response.Cookies["sessionstarttime"];
            cookies.Value = "Value of Cookies";
            cookies.Expires = DateTime.Now.AddMinutes(20);
            cookies.Path = "/";

if you are using JQuery then use following

$.cookie("sessionstarttime", "Value",{ path: '/' });

Upvotes: 0

matt_lethargic
matt_lethargic

Reputation: 2786

This is a very basic example showing how to set the cookie path.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ControllerContext.HttpContext.Response.Cookies.Add(
             new HttpCookie("test", "hello") { Path = @"/admin", 
             Expires = DateTime.Now.AddDays(1)});

        return View();
    }
}

Upvotes: 4

Related Questions