我正在创建一个cookie并在其中获取文本框值。我需要使其过期,然后通过检查它是否已过期来打印消息。我正在执行以下操作
HttpCookie usercookie = new HttpCookie("userinfo");
Response.Cookies["gettingusername"].Value = textbox_username.Text;
Response.Cookies["gettingusername"].Expires = DateTime.Now;
Response.Cookies.Add(usercookie);
if (Request.Cookies["gettingusername"]!=null)
{
Response.Write("Cookie is Not Expired");
}
else
{
Response.Write("Cookie Expired");
}
但它总是说: -
Cookie is Not Expired
我是新手。请帮助。 提前谢谢。
答案 0 :(得分:3)
你可以这样做:
myCookie.Expires = DateTime.Now.AddDays(-1);
if(myCookie.Expires > DateTime.Now)
Response.Write("Cookie not Expired");
else
Response.Write("Cookie Expired");
答案 1 :(得分:1)
浏览器负责删除过期的Cookie。您只需要在将来设定它将过期的时间。例如:
if (Request.Cookies["gettingusername"] != null)
{
Response.Write("Cookie is not expired")
Response.Write("Value exists : " + Request.Cookies["gettingusername"]);
}
else
{
Response.Write("Cookie is expired, creating a new cookie.");
Response.Cookies.Add(new HttpCookie("gettingusername")
{
Value = textbox_username.Text,
Expires = DateTime.Now.AddDays(1)
});
}
答案 2 :(得分:0)
尝试以下方法......
if (Request.Cookies["gettingusername"] != null)
{
HttpCookie myCookie = new HttpCookie("userinfo");
myCookie.Expires = DateTime.Now.AddDays(-1);//add -1 days
Response.Cookies.Add(myCookie);
}
答案 3 :(得分:0)
无法准确理解你想要实现的目标。 您应该设置它们并检查它们是否以不同的方法过期。
例如,您可以在按“设置Cookie”按钮时设置Cookie并在操作中使其过期'Expire Cookie'
答案 4 :(得分:0)
这是您现在可以删除 cookie 的方法..!
<ul class="navbar-nav">
<li class="nav-item">
<router-link class="nav-link" :to="{
path: '/',
hash: 'home',
}" :active='$route.name =="home"'>Home</router-link>
</li>
<li class="nav-item">
<router-link to="/About" :active='$route.name =="about"' class="nav-link">About</router-link>
</li>
<li class="nav-item">
<a class="nav-link" v-b-modal.modal-contact>Contact</a>
</li>
</ul>