在asp.net C#中创建简单的cookie [英] creating simple cookies in asp.net c#

查看:101
本文介绍了在asp.net C#中创建简单的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要存储cookie.当用户登录时,我想确保如果cookie不存在,请创建它并存储它的值,但是如果它确实对其进行了修改.

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.

if(cookieExist)
 {
    cookiename = "value";
 }
else
 {
   create a new cookie 
   then store the value;
 }

感谢您的帮助

推荐答案

您必须使用 Request.Cookies 获取cookie值,并使用 Response.Cookies 添加cookie

You have to use Request.Cookies to get cookie value and Response.Cookies to add cookies

 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
 }

这篇关于在asp.net C#中创建简单的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆