2

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.

1
  • Can anyone help me on this issue? Commented Apr 22, 2015 at 9:58

2 Answers 2

4

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks matt..Your approach will work best when we are adding user defined cookies.. But there are certain cookies which are automatically added by MVC such as: 1) ASP.NET_SessionId 2) __RequestVerificationToken_L0lMLU1BUw2 3) .ASPXAUTH .. etc. Approach suggested by you will not work for these cookies... What I want is to add path to all cookies including auto-generated and custom added (ex..test cookie which you defined.)
0

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: '/' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.