delete jquery cookie from other page on button click asp.net mvc - cookies

How can we delete a cookie [jquery plugin cookie $.cookie()] or set it to null on one page from a diff page? I want to delete a cookie on page 2 from page 1 on button click. I tried so many diff ways and I was unsuccessful. please help. is there any work around?

A cookie is not tied to a page. It is stored on the client computer/browser and sent along each request to the server. So if you set it to null on one page it will be deleted and no longer sent.

Rather old question, but I found it so....
Make sure to include the path on the Delete to match the one on the Set
$.cookie('key', 'value', { path: '/' });
....
$.cookie('key', null, { path: '/' });

Related

How can I embed in my site a CAPTCHA from another site, resolve it MANUALLY, send back to the original site and get the TOKEN?

The intention is not to resolve CAPTCHA automatically. Every user of my site will have to resolve the CAPTCHA.
The intention is to use free data from another site. These data are public and free, but to avoid massive requests, they are protected with CAPTCHA.
This is what I've done but doesn't work:
Create a proxy.php that manage and forward the requests to the original site.
Copy all headers from the original request (request of the CAPTCHA) and add them to the proxy. So, this is the form to resolve the CAPTCHA:
xxx is my site, example.com is the site that I want to resolve captcha and get data:
<img id="imgCaptcha" src="https://xxx/proxy.php?curl=https://example.com/Captcha&type=image&lang=it" style="width:200px;">
<input type="text" id="captcha">
<button type="button" id="btn_resolve">Resolve</button>
On button click, send the input text and check if it is resolved:
xxx is my site, example.com is the site that I want to resolve captcha and get data:
$('#btn_resolve').on('click',function(e) {
e.preventDefault();
var captcha = $('#captcha').val();
$.get('https://xxx/proxy.php?https://example.com/Captcha&type=check&captcha='+captcha, function(data, status) {
alert(JSON.stringify(data));
});
});
The result is always {"result":false,"token":"","message":null}
I think that the problem is with JSESSIONID cookie that I set in the proxy.php, but seems filtered out from Chrome with this motivation: "This cookie was blocked because its path was not an exact match for or a superdirectory of the request url's path".
Honestly I've got not clear if I can do this and how to do this: it seems that last versions of Chrome blocked some coockies. How can I do this with PHP CURL bypassing Chrome filters?
I resolved it adding all needed cookies in proxy.php file.
Proxy.php forward the request using curl.
This is a good starting point for a cross domain proxy in PHP that uses CURL commands
PHP CORS Proxy by softius
Then you can read JSESSIONID from after requesting the CAPTCHA image, and forward it to the proxy and add it and the others to the request:
header('Set-Cookie: cross-site-cookie=name; SameSite=None; Secure');
header('Set-Cookie: XSRF-TOKEN=XXXXX');
if (isset($_REQUEST['jsessionid'])) {
setcookie("JSESSIONID", NULL, 0, "/");
header('Set-Cookie: JSESSIONID='.$_REQUEST['jsessionid']);
}

Setting before rendering html page go-gin

I am trying to set a cookie on an HTML page
func testCookie(c *gin.Context) {
c.SetCookie("test1", "testvalue", 10, "/", "", true, true)
c.HTML(200, "dashboard", gin.H{
"title": "Dashboard",
}
}
This should have set the cookie on the HTML page but it doesn't.
My server is running to serve https requests. I am not sure why I am not able to set cookies here. I am using google-chrome and ideally I should have been able to see the cookie there.
The issue is with your maxAge input. Your current code instructs the browser to delete your cookie in 10 seconds.
Gin is wrapping http.SetCookie and creating a http.Cookie for you. To better understand what is happening you should read through those two links.
MaxAge=0 means no 'Max-Age' attribute specified.
MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
MaxAge>0 means Max-Age attribute present and given in seconds

Django cookie age not setting

I am trying to create a way for my users to have a "remember me" option when they log in. However as it stands, no matter what I try, when I view the cookie in my browser, it just shows Expires: At end of session. So once I close my browser and come back to my page, I am logged out.
In my settings.py I have set
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_COOKIE_AGE = 10000000
Which I assume are what I need to do...
On the front-end (which is in AngularJS) I have the following for the cookie storage:
$http.post('http://localhost:8000/auth/login/', {email: this.login['arguments'][0]['email'], password: this.login['arguments'][0]['password']})
.success(function(response){
if (response.token){
$cookieStore.put('djangotoken', response.token);
$http.defaults.headers.common['Authorization'] = 'Token ' + response.token;
}
})
.error(function(data, status, headers, config){
$cookieStore.remove('djangotoken');
});
If someone could show me how to get my cookies to just stay for the designated age I set that would be greatly appreciated!
You're setting the cookie directly from Angular, so it has nothing to do with the Django session cookie age. You need to pass the expiration time when you set the cookie.
Unfortunately, the built-in Angular cookieStore api is unnecessarily restrictive and does not support this. It looks like a good alternative is angular-cookie.

Do all 404 pages share something in common?/Are 404 page distinguishable from regular pages?

Say I am running stumbleupon.com and users can submit sites to be added into the db of sites. Is there a way that I can write a program to see whether or not the site that was added by the user is actually a real website? Or if it bring me to a 404 that I can say "welp, that was some bugus" or maybe that the user made an error when submitting the url? Or do i need to put people on my payroll to see if the user submitted website bring me to a 404?
The response code from the request is a 404 not found, instead of a 200 success message that you get on a good response from the server. You can easily automate the check.
Since I don't know much about python, I offer a client side solution.
You could check if the URL is legit as soon as the user submits the page. Maybe throw up a 'verifying url' message or something.
Using this method you could make a cross domain call to see if that site is really there. Then you could pop up a message that asks them to fix their mistake or allow the URL to be submitted. This solution uses Yahoo as a proxy, but as you can see by my super simple function below it does work.
function doAjaxCheck(url){
// if the URL starts with http
if(url.match('^http')){
//add a 'loading message here, or something'
// assemble the YQL call
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=xml'&callback=?",
function(data){
if(data.results[0]){
alert('it is OK');
} else {
alert('not OK');
}
}
);
} else {
$.ajax({
url: url,
timeout:5000,
success: function(data){
alert('it is OK');
},
error: function(req,error){
if(error === 'error'){error = req.statusText;}
alert('not OK : ' + error );
}
});
}
}
However, all this being said, I would go with a server-side check for reliability-sake.

How to save (temporarily) form data?

There are, say, 10 fields on page1 and hyperlink to page2. Also there is hyperlink on page2 to page1. I fill 5 fields and click on the hyperlink. Then I click on the hyperlink on page2 and return to page1. Is it possible to save filled fields and how?
Additional question: what if page2 modifies fields of page1. For example, creates new choice in multichoice field.
Django has implemented solution which allow to split forms on multiple web pages. It is called form wizard. Check here for the tutorial.
EDIT 1#
Check this questions up: Django Passing data between views, How do you pass or share variables between django views?
You can save filled fields using cookies via javascript after clicking on the link and before going to another page. For example it's possible to use this jQuery plugin jQuery-cookie. As documentations says:
A simple, lightweight jQuery plugin for reading, writing and deleting cookies.
Create session cookie:
$.cookie('the_cookie', 'the_value');
Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });
Create expiring cookie, valid across entire page:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
Read cookie:
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null
Delete cookie by passing null as value:
$.cookie('the_cookie', null);
Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie.