ASP NET 2.0 Cookie [英] ASP NET 2.0 Cookie
本文介绍了ASP NET 2.0 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在ASP网站上运行的C#实用程序。当用户登录
时,ASP代码使用Response.Cookie写入cookie。我尝试在C#中读取
相同的cookie。我无法用C#代码阅读它。
我正在使用Request.Cookies [" cookiename"]。Value.ToString()。我可以在ASP代码中读取
相同的cookie。
如果我说ASP中的Request.Cookies(CookieName)工作正常..
I have a C# utility that runs in ASP website. When user logs in the
ASP code writes the cookie using Response.Cookie. I try to read the
same cookie in C#. 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..
推荐答案
re:
!我正在使用Request.Cookies [" cookiename"]。Value.ToString()
尝试以下两种方法之一:
1.
if(Request.Cookies [" cookieName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies [" cookieName"]。Value);
2.
if(Request.Cookies [" cookieName"]!= null)
{
HttpCookie aCookie = Request.Cookies [" cookieName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
在尝试获取a的值之前cookie,你应该确保cookie存在;
如果cookie不存在,你将得到一个NullReferenceException异常。
另请注意HtmlEncode米ethod被调用来编码cookie的内容
,然后在页面中显示它。这可以确定恶意用户没有在cookie中添加
可执行脚本。
Juan T. Llibre,asp.net MVP
asp.net faq: http://asp.net.do/faq/
foros de asp.net,en espa?ol: http://asp.net.do/foros/
========================= =============
< ic ******** @ gmail.com在消息新闻中写道:17 ********* ************************* @ v26g2000 prm.googlegroups.com ...
re:
!I am using Request.Cookies["cookiename"].Value.ToString()
Try either of these approaches :
1.
if(Request.Cookies["cookieName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["cookieName"].Value);
2.
if(Request.Cookies["cookieName"] != null)
{
HttpCookie aCookie = Request.Cookies["cookieName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
Before trying to get the value of a cookie, you should make sure that the cookie exists;
if the cookie does not exist, you will get a NullReferenceException exception.
Notice also that the HtmlEncode method was called to encode the contents of a cookie
before displaying it in the page. This makes certain that a malicious user has not added
executable script into the cookie.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espa?ol : http://asp.net.do/foros/
======================================
<ic********@gmail.comwrote in message news:17**********************************@v26g2000 prm.googlegroups.com...
>我有一个在ASP网站上运行的C#实用程序。当用户登录
时,ASP代码使用Response.Cookie写入cookie。我尝试在C#中读取
相同的cookie。我无法用C#代码阅读它。
我正在使用Request.Cookies [" cookiename"]。Value.ToString()。我可以在ASP代码中读取
相同的cookie。
如果我说ASP中的Request.Cookies(CookieName)工作正常..
>I have a C# utility that runs in ASP website. When user logs in the
ASP code writes the cookie using Response.Cookie. I try to read the
same cookie in C#. 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..
我知道如何阅读cookie。问题是,当我尝试在
C#中读取它时,它总是返回null。在ASP中,它返回cookie的值。
I know how to read the cookie. The problem is when I try to read it in
C# it always returns null. In ASP it returns the value of the cookie.
re:
!我知道如何阅读cookie。
!问题是当我尝试在C#中读取它时它总是返回null。
那么,你使用的代码都没有读过正确的cookie,
或cookie没有正确设置。
您是否尝试过我发布的代码来读取cookie?
至于设置cookie,这里有2个样本C#代码片段:
1.
Response.Cookies [" ; userName"] .Value =" patrick";
Response.Cookies [" userName"]。Expires = DateTime.Now.AddDays(1);
2.
HttpCookie aCookie =新的HttpCookie(" lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
你如何设置饼干?
你到期了吗?
Juan T. Llibre,asp.net MVP
asp.net faq: http://asp.net.do/faq/
foros de asp。 net,en espa?ol: http://asp.net.do/foros/
====================================== />
< ic ******** @ gmail.com在消息新闻中写道:11 ************************ ********** @ z16g2000 prn.googlegroups.com ...
re:
!I know how to read the cookie.
!The problem is when I try to read it in C# it always returns null.
Then, either the code you''re using doesn''t read the cookie properly,
or the cookie isn''t being set correctly.
Have you tried the code I posted to read cookies ?
As for setting cookies, here''s 2 sample C# code fragments :
1.
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
2.
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
How are you setting the cookie ?
Are you expiring the cookie ?
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espa?ol : http://asp.net.do/foros/
======================================
<ic********@gmail.comwrote in message news:11**********************************@z16g2000 prn.googlegroups.com...
>我知道如何阅读cookie。问题是,当我尝试在
C#中读取它时,它总是返回null。在ASP中,它返回cookie的值。
>I know how to read the cookie. The problem is when I try to read it in
C# it always returns null. In ASP it returns the value of the cookie.
这篇关于ASP NET 2.0 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文