admin管理员组

文章数量:1384294

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 pleted,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.

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 pleted,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.

Share Improve this question edited Aug 29, 2012 at 11:13 F.P 17.9k34 gold badges126 silver badges194 bronze badges asked Aug 29, 2012 at 11:10 SoftBuilders PkSoftBuilders Pk 1094 silver badges15 bronze badges 1
  • Can you access the cookie in JS after it's been created? – Strillo Commented Aug 29, 2012 at 11:23
Add a ment  | 

3 Answers 3

Reset to default 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./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.

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);
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);
    }        
}

本文标签: