Localhost cookie in ASP.NET C# - c#

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

Related

Read cookie value with c#

using asp.net/c#, how is possible to :
display a webform only when the cookie value has been set to "1". (on page_load event)
So the asp code should just read the cookie value and make visible/invisible the webform
Note that the cookie value I will set it with php.
Note that the html webform is included in the code:
asp:Content ID="webform" ContentPlaceHolderID="webform1" runat="Server"
So I need a way to manipulate this webform depending of the settings of the cookie value that I will read all the time the page gets loaded
if (!Page.IsPostback)
Call the method below, based on the return value response.redirect to another page.
private string GetCookieValue(string cookieName, string itemName)
{
var CookieName = "MY_COOKIE";
var CookieValue = string.empty;
HttpCookie myCookie = Request.Cookies[CookieName];
if (myCookie == null) return "No cookie found";
//If you added a key vs. the value in the cookie use this code
//CookieValue = myCookie[itemName].ToString();
//Get the value of the cookie if you are not using a key
CookieValue = myCookie.Value.ToString();
Return CookieValue;
}

C# persistent cookie

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.

How to immediately remove cookies from browser

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.

Problem with HttpOnly Cookies

I have a problem with creating HttpOnly Cookies , I use the following code to creat new cookie:
//A.aspx
HttpCookie ht = new HttpCookie("www");
ht.Value = "www";
ht.Name = "www";
ht.HttpOnly = true;
ht.Expires = DateTime.Now.AddDays(1);
Response.AppendCookie(ht);
Response.Redirect("B.aspx");
//B.aspx
HttpCookie cookie = Request.Cookies["Allowed"];
HttpCookie htt = Request.Cookies["www"];
if (cookie != null)
{
Response.Write(cookie.HttpOnly);
Response.Write(htt.HttpOnly);
}
else
{
cookie = new HttpCookie("Allowed");
cookie.HttpOnly = true;
cookie.Value = "ping";
cookie.Expires = DateTime.Now.AddMinutes(2);
Response.Cookies.Add(cookie);
Response.Write(cookie.HttpOnly);
Response.Write(htt.HttpOnly);
}
The problem is that the final result is always : False, although the HttpOnly property is set to True .
Can anyone explain me a way to figure this out ?
Thanx
Cookie parameters (expiration date, path, HttpOnly etc) are not sent back to the server by the browser, only the values. Sending them back would only introduce unnecessary bloat. Therefore the cookies in Request.Cookies will only contain the names and values.
If you want to see if your HttpOnly value is taking effect, use Firecookie or something similar to inspect the cookies. Or try accessing them in JavaScript - that's what it's supposed to prevent.

The difference between HttpCookie and Cookie?

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.

Categories