471,356 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

ASP cookie reading using C#

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
3 2128

<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
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
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.

Similar topics

3 posts views Thread by georges the man | last post: by
1 post views Thread by 4site | last post: by
3 posts views Thread by ambrish | last post: by
1 post views Thread by shaahid786 | last post: by
4 posts views Thread by boonboy28 | last post: by
1 post views Thread by artic | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.