So I'm confused as msdn and other tutorials tell me to use HttpCookies to add cookies via Response.Cookies.Add(cookie). But that's the problem. Response.Cookies.Add only accepts Cookies and not HttpCookies and I get this error:
cannot convert from 'System.Net.CookieContainer' to 'System.Net.Cookie'
Additionally, what's the difference between Response.Cookies.Add(cookie) and Request.CookieContainer.Add(cookie)?
Thanks for the help in advance, I'm trying to teach myself using C#.
// Cookie
Cookie MyCookie = new Cookie();
MyCookie.Name = "sid";
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
// HttpCookie
HttpCookie MyCookie = new HttpCookie("sid");
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
Response.Cookies.Add(MyCookie);
You are using System.Net.HttpWebResponse. But the above example uses System.Web.HttpResponse which takes System.Web.HttpCookie as a parameter.
Scott Allen
System.Web.HttpRequest is a class used
on the server and inside an ASP.NET
application. It represents the
incoming request from a client.
System.Net.HttpWebRequest is a class
used to make an outgoing request to
a web application.
Related
I have seen the persistent cookies examples in ASP.NET MVC C# here on stackoverflow.
But I can't figure out why the code below isn't working.
First I write to the cookie:
HttpCookie cookie = new HttpCookie("AdminPrintModule");
cookie.Expires = DateTime.Now.AddMonths(36);
cookie.Values.Add("PrinterSetting1", Request.QueryString["Printer1"]);
cookie.Values.Add("PrinterSetting2", Request.QueryString["Printer2"]);
cookie.Values.Add("PrinterSetting3", Request.QueryString["Printer3"]);
Response.Cookies.Add(cookie);
I see the cookies stored in Internet Explorer. The content looks OK.
Then the reading code:
HttpCookie cookie = Request.Cookies["AdminPrintModule"];
test = cookie.Values["PrinterSetting2"].ToString();
The cookie variable keeps null . Storing the PrinterSetting2 value in the test variable fails.
I don't know what I'm doing wrong because this is more or less a copy-paste from the examples here on stackoverflow. Why can't I read the PrinterSetting2 value from the cookie ?
try with below code :-
if (Request.Cookies["AdminPrintModule"] != null)
{
HttpCookie cookie = Request.Cookies["AdminPrintModule"];
test = cookie["PrinterSetting2"].ToString();
}
Have a look at this document http://www.c-sharpcorner.com/uploadfile/annathurai/cookies-in-Asp-Net/ :-
Below are few types to write and read cookies :-
Non-Persist Cookie - A cookie has expired time Which is called as
Non-Persist Cookie
How to create a cookie? Its really easy to create a cookie in the
Asp.Net with help of Response object or HttpCookie
Example 1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));
Response.Cookies.Add(userInfo);
Example 2:
Response.Cookies["userName"].Value = "Annathurai";
Response.Cookies["userColor"].Value = "Black";
How to retrieve from cookie?
Its easy way to retrieve cookie value form cookes by help of Request
object. Example 1:
string User_Name = string.Empty;
string User_Color = string.Empty;
User_Name = Request.Cookies["userName"].Value;
User_Color = Request.Cookies["userColor"].Value;
Example 2:
string User_name = string.Empty;
string User_color = string.Empty;
HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
User_color = reqCookies["UserColor"].ToString();
}
You must ensure that you have values in Request.QueryString.Just to check if your code works hard code values of cookies and then read from cookie.
I am trying to write cookie in ASP.NET using C# with this simple code:
HttpCookie cookie = new HttpCookie("UserToken");
cookie.Value = userToken.access_token; //string
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
Cookie was build. I am using Chrome browser but the value (Konten) always empty. What's the problem?
I also try to read this cookie with this code:
if (Request.Cookies["UserToken"] != null)
string userToken = Request.Cookies["UserToken"].Value;
"UserToken" cookie is not null but the value was empty string.
Thanks in advance
I am using cookies for my website.
I need to remove cookies immediately from browser for that i use this Code in C#
Code for removal of cookie
Response.Cookies["OptDepth"].Expires = DateTime.Now.AddYears(-30);
after execution of this code if i use this code
Code to check value of deleted cookie
Request.Cookies["OptDepth"].value;
then it gives me the value of specified cookie.
I need to remove cookies immediately from browser.
How can i do this.
If we change the name of that cookie with null then it works.
Code
Request.Cookies["OptDepth"].Name = null;
Now problem is solved.
Use Simple Code to replace cookies with expiry date:
if (Request.Cookies["OptDepth"] != null)
{
HttpCookie myCookie = new HttpCookie("OptDepth");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
Here is an msdn detail link.Cookies
When adding a cookie ;
HttpCookie cookie = new HttpCookie("try");
cookie.Values["foo"] = "foo";
DateTime date = DateTime.Now.AddSeconds(-30);
cookie.Expires = date;
Response.Cookies.Add(cookie);
When Reading the cookie;
HttpCookie cookie = Request.Cookies["try"];
You will see the cookie is null.
I'm integrating a single sign on over 2 ASP.Net applications. For that matter i have a web service that is called by the main app. when a user logs in. this web service authenticates the user in my second application and brings back the authentication cookies i need to deliver to the client browser so he can navigate freely and logged in both applications.
I was planning to use HttpContext.Current.Response.Cookies.Add(cookie) in order to deliver the new cookies but this seems not to work as no cookies are added what so ever...
Any ideas on what might be going wrong?
here is my code:
var service = new localhost.UserManagement();
service.CookieContainer = new CookieContainer();
if (service.AuthenticateUser("test#user.pt", "test"))
{
var collection = service.CookieContainer.GetCookies(new Uri("http://localhost"));
foreach (Cookie item in collection)
{
HttpContext.Current.Response.Cookies.Add(CookieConverter(item));
}
HttpContext.Current.Response.Flush();
return true;
}
return false;
Note: CookieConverter(item) is used to convert Cookie object i receive to HttpCookie
Thanks
private HttpCookie CookieConverter(Cookie cookie)
{
var result = new HttpCookie(cookie.Name);
result.Value = cookie.Value;
result.Domain = cookie.Domain;
result.Expires = cookie.Expires;
result.Path = cookie.Path;
result.Secure = cookie.Secure;
result.HttpOnly = cookie.HttpOnly;
return result;
}
You should check:
collection is empty? Could you set braeakpoint and check collection?
where is this code located? (.aspx page, web service, http handler?)
try to create minimalistic "Cookie setter" that just add simple cookie in any way
I have a code as follows:
this.Response.Cookies.Add(new HttpCookie("COOKIENAME",'test'));
I want to add the domain ".test.com" for this cookie. How do I do so? I tried the standard:
this.Response.Cookies["COOKIENAME"].Domain = ".test.co.uk";
But the cookie is not being set for the whole domain. Any suggestions?
The following is not working either:
HttpCookie MyCookie = new HttpCookie("COOKIENAME");
MyCookie.Value = 'test';
MyCookie.Domain = ".test.co.uk";
this.Response.Cookies.Add(MyCookie);
I don't think you need the ".", so just MyCookie.Domain = "test.co.uk";.
I think you should use the Cookie Path property to define the domain of the cookie