3

I am working with cookies and I happened to create it using JavaScript ,but when I try to expire that cookie after my process is completed,using C# code behind file ,there I am unable to find the specified Cookie??

What could be the reason for this?? I think cookies created in JavaScript are not accessible/Visible using C# ...? Is that true??

Here is my code for creating cookie in JS

var expiryDate = new Date();
expiryDate.setTime(expiryDate.setDate(expiryDate.getDate() + 1)); // 365 days
document.cookie = "ReferedCookie=" + "clientId=" + UserGuid + "&productId=" + productId + "&Token=" + token + ";" + "expires=" + expiryDate.toGMTString() + ";";

and here is my C# code for finding and expiring cookie

public void DeleteCookie(string Name) 
{
    if (System.Web.HttpContext.Current.Request.Cookies["ReferedCookie"] != null)
    {
        HttpCookie myCookie = new HttpCookie(Name);
        myCookie.Expires = DateTime.Now.AddDays(-5d);
        System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
    }        
}

Thanks in advance.

1
  • Can you access the cookie in JS after it's been created? Commented Aug 29, 2012 at 11:23

3 Answers 3

5

The problem is likely with the Path property of the cookie.

When setting a cookie using Javascript, the default path of the cookie will be based on the location of the page that sets the cookie.

In order to expire that cookie, you must specify the same path. So if you have a page:

http://test.foo.com/somepath/default.asxp

and you use the javascript code you had in your question to set a cookie on this page, the default path of the cookie will be:

/somepath/

This means the browser will send this cookie to all pages that are under that path. It will not be sent to pages outside that path.

To expire this cookie from the server, you need to specify the path of the cookie:

HttpCookie myCookie = new HttpCookie(Name);   
myCookie.Expires = DateTime.Now.AddDays(-5d);   
myCookie.Path = "/somepath/";   
System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);   

Alternatively you must specify the path when originally setting the cookie to (for instance) /:

document.cookie = "ReferedCookie=" + "clientId=" + UserGuid + "&productId=" + productId + "&Token=" + token + ";" + "expires=" + expiryDate.toGMTString() + ";path=/";

and then expire it on the same path.

Sign up to request clarification or add additional context in comments.

3 Comments

@user1429080 Im having a similar issue and need to be able to access the cookie from 2 paths eg: 'root/path1' and 'root/path2'. Is this possible? If so, how? Thanks.
@phill healey: Yes it's possible. The easiest way is to specify the path '/' when originally setting the cookie. Or possibly (using your example) '/root/'
@user1429080 Thanks for the response. I was using root as literally meaning the root of the website. After much fiddling Ive found the only way to access the cookie is by setting the path as empty(eg no even using a slash).
0

It nice to use a function for this, to minimize the chance on errors:

function set_cookie(name, value, expires, path, domain, secure)
{
  var cookie_string = name + "=" + escape ( value );

  if(expires)
  {
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if(path)
        cookie_string += "; path=" + escape ( path );

  if(domain)
        cookie_string += "; domain=" + escape ( domain );

  if(secure)
        cookie_string += "; secure";

  document.cookie = cookie_string;
}

And then in your case call it like this:

var value = "clientId=" + UserGuid + "&productId=" + productId + "&Token=" + token;
var expiryDate = new Date();
expiryDate.setTime(expiryDate.setDate(expiryDate.getDate() + 1)); // 365 days

set_cookie("ReferedCookie", value, expiryDate);

2 Comments

Dear friend..here you are expiring the cookie in JS file, which i dont need to do that, I need to expire it using C# code and the cookie is creating very nicely with the code you provided...kindly figure out why my C# code is not working, even the cookie is generated on Client side,which i can see using web developer toolbar
Can you see what path and domain the cookie uses? and compare that to the path and domain of your aspx page. The domain have to equal or a subdomain, the path have to be equal or a child of that path. This is necessary otherwise the browser will not send the cookie to the server.
0
DeleteCookie("ReferedCookie");


public void DeleteCookie(string Name) 
{
    if (System.Web.HttpContext.Current.Request.Cookies[Name] != null)
    {
        HttpCookie myCookie = new HttpCookie(Name);
        myCookie.Expires = DateTime.Now.AddDays(-5d);
        System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);
    }        
}

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.