I am trying to comply with new EU law for cookie legislation and need to remove all the cookies on my site before the user consents to it. Currently I've used the following successfully:
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
However once this process runs there is still as a cookie called ASP.NET_SessionID which is created by ASP.NET. To overcome this I set the following in the web.config file which basically stores the sessionID in the URL:
<sessionState regenerateExpiredSessionId="true" cookieless="true" />
The problem I have is when I set cookieless="true" the javasscript code above doesn't remove any of my cookies anymore, its like its become redundant. When I set cookieless="false", the javascript works perfectly. Are there any other settings I need to change to get both to work together?
Thanks
If you don't need session, you can disable sessionstate (mode="Off")
Also, I usually cleanup the cookies server-side, like this (never looked if it removed asp.net sessionid though) :
protected void Page_Load(object sender, EventArgs e)
{
Response.BufferOutput = true;
Session.Abandon();
HttpCookieCollection cookies = Request.Cookies;
var cookiesList = new List<String>();
foreach (String cookieKey in cookies)
cookiesList.Add(cookieKey);
foreach (var cookieKey in cookiesList)
{
var cookie = new HttpCookie(cookieKey);
cookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(cookie);
}
}
Hope this can help.
Related
I want to get the Browser cookies using JavaScript.I tried the below code , but i am not getting the cross domain cookies.
Here is the code:
function get_cookies_array() {
var cookies = {};
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
return cookies;
}
var cookies = get_cookies_array();
for (var name in cookies) {
document.write(name + " : " + cookies[name] + "<br />");
}
Does anybody solve this.
In most situations, you cannot read cross domain cookies, for security reasons.
Each cookie has a domain of definition, and your browser reads those to decide which cookies you can read according to which domain you're on.
If you have control of both domains, you can modify cookie settings on domain B to allow them to be read by domain A, or code a cookie getter to get the values. Be creative!
I'm having problem of creating a cookie, I have this code:
window.onload = function() {
var value = readCookie('username');
if(value == null){
alert("null");
document.cookie = "username=Bob; expires=Thu, 18 Dec 2016 12:00:00 UTC";
}
else
alert(value);
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
When the page load I check if the cookie exists, if it doesn't, I pop up an alert saying that is null, then I create the cookie, but every time I load the page it always says its null. Now the problem is in the creation of the cookie or in the readCookie function. I can't find why this doesn't work.
UPDATE
So in google Chrome it won't work, but in internet explorer works perfectly, someone knows why? I would like to work in all browsers.
When I create an HTML page using that code and run it in a normal test environment, the problem you describe does not occur. It alerts null on the first load and Bob on the second load.
If, however, I tell the browser to load the page from file:///Users/david/tmp/jgfklgjkl/index.html instead of my test server at http://localhost:7007/, then it always alerts null.
Cookies are associated with a hostname, and you have to load the page over HTTP in order to use them.
You are presumably loading them from a local file, and that is where the problem lies, not with your code.
Try this function for reading cookie. I've been using it for quite too long and it's working fine so far
function getCookieValue(cookie_name) {
var cookie, cookies = document.cookie.split(';');
for (var i = cookies.length - 1; i >= 0; --i) {
cookie = cookies[i].split('=');
if (cookie[0].trim() === cookie_name)
return cookie[1];
}
return "";
}
Also if you are interested you could use this function for adding cookies for 10 years.
function addCookie(name, value) {
var expire = new Date();
expire.setFullYear(expire.getFullYear() + 10);
d.cookie = name + "=" + value + "; expires=" + expire.toGMTString() + "; path=/";
}
I've made a cookie using asp.net C# code and I want to retrieve its value in javascript
Here's my C# code:
HttpCookie cookie = new HttpCookie("doc1count");
cookie.Value = "hellonitesh";
I am using this code:
var cookie = '#HttpContext.Current.Request.Cookies["doc1count"].Value';
alert(cookie);
but its giving me garbage value.
Try to separate client and server side
Add cookies to responce on server:
HttpCookie myCookie= new HttpCookie("doc1count");
myCookie.Value = "hellonitesh";
HttpContext.Response.Cookies.Add(myCookie);
And retrieve it in javascript (You can find more examples
here)
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
var myCookie = getCookie("doc1count")
Try to use jquery.cookie plugin to manage cookie at client end. Apart from this, I can see in your C# code you are not posting the cookie variable. So you should do like following -
HttpContext.Current.Response.Cookies.Add(new HttpCookie("cookie_name","cookie_value"));
You can probably just access it through ASP.NET variable by adding in the inline code I have right here. That's if you wanna be fairly simple about it, you can check if it contains anything with an if statement before assigning it I believe.
var cookie = '<%= Request.Cookies["cookieID"].ToString() %>';
alert(cookie);
Try this
var cookie = '#Request.Cookies["culture"].Value';
I created a function to get the cookie in javascript :
function getCookie() {
var arr = document.cookie.split(";");
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, arr[i].indexOf("=")).replace(/^\s+|\s+$/g, "") == "taxibleC") {
return arr[i].substr(arr[i].indexOf("=") + 1);
}
}
}
var multipleVAT = 1;
And then I have another function to initialize the cookie :
function ChangeVATValue()
{
if ($("#vatEnable").is(':checked')) {
multipleVAT = 1;
} else {
multipleVAT = 0;
}
document.cookie = "taxibleC=" + multipleVAT;
alert(getCookie());
}
When I used alert(getCookie());, It has the value 1.
But when I click to another page, the alert is 0.
Could anyone tell me, why I cannot access the session by using the getCookie() method in the view of my asp.net MVC 3.0 project.
That is because you cookie might get expire immediatly, if possible se cookie expiration time to certail limit and than access the value of cookie on another page that resolve your issue
something like
document.cookie =
'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
You need to set path in the cookie for accessing in different page
;path=/
For example,
document.cookie = 'YOUR COOKIE DATA;path=/'
I have set a cookie with document.cookie. The content settings in Google Chrome is showing the cookie. However, document.cookie is displaying as a blank string when printed. Why is this happening?
Here is my code
function setCookie(name,value,lifeTime,path,domain,secure){//lifetime in hours
{
var c_ = name +'='+escape(value)+'; ';
var life = new Date();
lifeTime<1&&lifeTime>0?life.setMinutes(life.getMinutes()+lifeTime*60):life.setHours(life.getHours()+lifeTime);
life = life.toUTCString();
c_+='expires='+life+"; ";
c_+= 'domain='+domain+'; ';
c_ += 'secure=secure; ';//secure
c_ += 'path='+path;
document.cookie = c_;
alert(document.cookie);
/*Just splitted the code instead of c = 'name='+value+'expires ='+life etc*/
}
A possible problem with this function is, it always sets a secure cookie. So, if you've requested/opened the page with HTTP not HTTPS protocol, the secure cookie won't be exposed.