By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

ASP cookie reading using C#

P: n/a
I have a C# utility that runs in ASP website. when user logs in the
ASP code writes the cookie. I am unable to read it in C# code.

I am using Request.Cookies["cookiename"].Value.ToString()
I can read the same cookie in ASP code.
If I say Request.Cookies("CookieName") in ASP that works fine..
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<ic********@gmail.comwrote in message
news:20**********************************@n1g2000p rb.googlegroups.com...
>I have a C# utility that runs in ASP website. when user logs in the
ASP code writes the cookie. I am unable to read it in C# code.

I am using Request.Cookies["cookiename"].Value.ToString()
I can read the same cookie in ASP code.
If I say Request.Cookies("CookieName") in ASP that works fine..
You should post to ms.public.dotnet.framework.aspnet.

Jun 27 '08 #2

P: n/a
A page with the below code reads cookies stored on a user's browser.

Hope this may provide solution/idea for your query.
----------------------------------------------------
<%@ language="C#" %>
<script runat="server">
void ReadClicked(Object Sender, EventArgs e)
{
//Get the cookie name the user entered
String strCookieName = NameField.Text;
//Grab the cookie
HttpCookie cookie = Request.Cookies[strCookieName];
//Check to make sure the cookie exists
if (null == cookie)
{
Response.Write("Cookie not found. <br><hr>");
}
else
{
//Write the cookie value
String strCookieValue = cookie.Value.ToString();
Response.Write("The " + strCookieName + " cookie contains: <b>" + strCookieValue + "</b><br><hr>");
}
}
</script>
<html>
<body>
Use the button below to read a cookie<br>
<form runat="server">
Cookie Name <asp:textbox id="NameField" runat="server" />
<asp:button text="ReadCookie" onclick="ReadClicked" runat="server" />
</form>
<a href="writecookies.aspx">Write Cookies</a>
</body>
</html>
----------------------------------------------------

You can store multiple strings in a cookie using the HttpCookie class. This class have few advanced properties.
Jun 27 '08 #3

P: n/a
Reading a Cookie:

HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];

// Read the cookie information and display it.
if (myCookie != null)
Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
Response.Write("not found");
Refere this link for more info :
http://www.csharphelp.com/archives/archive179.html
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.