When I add a cookie through Response.cookies.add(cookie); My cookie will not be placed on the clients side until the client requests a page from my site. At the time of request .net will do some magic, place the cookie in the response and the client will store it. Is this true? If the above assumption is true I should be able to see unplaced cookies by cookie = response.cookies("foo"). Seems logical, but is it correct?
To sum it up.
I am placing a cookie, then later in the code before the request is served I am checking if the cookie is in the request.cookies("foo") if it is not I am checking the response.cookies("foo"). This method does not work. How would i go about reading a cookie before it is sent to the client side.
The actual question I need answered; Is there a way to view a cookies information before I send it to the browser? Something along the lines of check if cookie is on browser if not do some other check to see if it is waiting to be sent.If it is waiting to be sent read data on it
thank you very much.
If I understood the question correctly then you want to add a cookie to HttpResponse at some point after receiving a request from a client. Then at a later point of processing the request you want to access the same cookie again.
This quote might help you:
"After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client."
(from http://msdn.microsoft.com/en-us/library/system.web.httpresponse.cookies.aspx)
I suggest you to process the cookie only at one point during the response. Thus you can check if it's available in HttpRequest, and if not then add it to HttpResponse and invoke your additional logic.
Related
I'm using httpclient to request a ssl(https://) site. But I note that even if I can get response from that site, but the site still hasn't set cookie in my local. So, I cannot read cookie from filter.CookieManager.GetCookies().
I guess that's due to I have not add certificate when posting data.
I done another testing, I used webview to open this site, then I can get cookies by CookieManager.GetCookies.
So, my question is how I can use httpclient to request "https://" site and make it write cookie in my local.
Read the documentation here for the cookies from response. There is also a working example available.
Similar:
Multiple cookies with same name
How to handle multiple cookies with the same name?
I am setting a cookie on the page load of my asp control. I have a function gets the cookie and stores it in a var to be used. My question is, after reading the msdn site on cookies, is should I get the cookie from the Request or the Response and then set value of the other? Or should I get the cookie from the Response and set it's value? The last one doesn't seem quite right but I don't know.
MSDN text:
The browser is responsible for managing cookies on a user system. Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection. When creating a cookie, you specify a Name and Value. Each cookie must have a unique name so that it can be identified later when reading it from the browser. Because cookies are stored by name, naming two cookies the same will cause one to be overwritten.
I assumed that I should get it from the request and set the response cookie, that doesn't seem to be quite right because I have ended up with double cookies with different values. The cookie I am worried about is TRACKINGINFO. It is set on a different sub domain and I need to pick up the value and just add to it and re-set the value.
Here is the request headers from Google Chrome:
ASP.NET_SessionId=bi2ingjjk5f4nhm; referer_domain=dev-znode.local; referer_query=; TRACKINGINFO=1234%2C526%2C1234%2C526; __utma=251778844.1427755012.1389292549.1389292549.1389292549.1; __utmb=251778844.16.10.1389292549; __utmc=251778844; __utmz=251778844.1389292549.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ZNodeCookie; LocaleID=43; TRACKINGINFO=1234%2C526%2C1234%2C526,2508
Request headers:
TRACKINGINFO=1234%2C526%2C1234%2C526,2500; expires=Fri, 10-Jan-2014 19:21:53 GMT; path=/
Yes, you should get cookies from request and set them on response.
There is magic code in HttpResponse that clones new cookies to request (and in your case probably does it in some way you don't expect) so developers can somewhat randomly pick where to read cookies from on server.
It may be cleaner to read cookies early in request lifetime (before any cookies set on response) and set cookies on response late (but not too late as it have to happen before first portion of response body flushed to client).
I would like to know can we delete cookie from cookies collection what we have created in asp.net website.I tried & find Expiration Logic.It works but it shows in browser cookie.
Response.Cookies["UserID"].Expires = DateTime.Now.AddDays(-1);
Is there any other way by this we can delete cookies from collection so it will not show in browser cookies.
Please help me to solve the issue.Thanks in advance.
From the documentation:
You cannot directly delete a cookie on
a user's computer. However, you can
direct the user's browser to delete
the cookie by setting the cookie's
expiration date to a past date. The
next time a user makes a request to a
page within the domain or path that
set the cookie, the browser will
determine that the cookie has expired
and remove it.
So, your strategy is the right one, and the cookie should disappear from the browser once the response is received.
I'm not sure you can delete the cookie since you don't have access to delete anything on the client computer. All you can do is basically what you are doing, that is invalidating the cookie for your application. I think it is up to the client software to decide if the cookie should be deleted or not, all you can do is set the timestamp as you are doing and that means that you will no longer accept that cookie.
I have a program where I want to scrap some useful study material for personal use.
This site maintain a session key and some other key also.
If I tried to go to a nested page then it will end the session.
I'm unable to maintain session key using a web request class.
How can I maintain a session using a web request class?
Please help.
You need to maintain the CookiesCollection across your requests.
var request = (HttpWebRequest)HttpWebRequest.Create("http://www.mysite.com/");
var cookies = new CookieContainer();
//Pass the collection along with each request to maintain session
request.CookieContainer = cookies;
When you get the response back, your container will automatically contain the set of cookies associated with it. You can then reuse that container with subsequent requests.
Essentially you will want to read the session cookie from the response header and pass it back in the header with every request you issue. There may be more to it depending on the application you are targeting.
Got a similar problem when embedding a Page in an iFrame. Solution is, for security purposes sessions get discarded by browsers (mainly IE8). Maybe this is a splution? Google for P3P to get more info.
-sa
I login to a website by HTTPwebrequest, I get an authentication cookie.
When I make a new HTTP-webrequest and add the cookies from the first request to call the next page where I can see my personal data. I see that the cookies will associated with the second request if I debug it but if I check the network traffic I see that are no Cookies transmitted at the second request. I tried many possibilities to see why I don't work but I found nothing.
Does anyone know a solution?