在 asp.net c# 中创建简单的 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12079332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating simple cookies in asp.net c#
提问by user516883
My application needs to store cookies. When a user logs on I want to make sure that if the cookie does not exist create it and store value, but if it does modify it.
我的应用程序需要存储 cookie。当用户登录时,我想确保如果 cookie 不存在,则创建它并存储值,但如果它确实修改了它。
if(cookieExist)
{
cookiename = "value";
}
else
{
create a new cookie
then store the value;
}
Thanks for any help
谢谢你的帮助
采纳答案by Waqar Janjua
You have to use Request.Cookiesto get cookie value and Response.Cookiesto add cookies
您必须使用Request.Cookies来获取 cookie 值并Response.Cookies添加 cookie
string cookievalue ;
if ( Request.Cookies["cookie"] != null )
{
cookievalue = Request.Cookies["cookie"].ToString();
}
else
{
Response.Cookies["cookie"].Value = "cookie value";
Response.Cookies["cookie"].Expires = DateTime.Now.AddMinutes(1); // add expiry time
}

