如何将字符串存储在Cookie中并进行检索 [英] How to store string in a cookie and retrieve it

查看:167
本文介绍了如何将字符串存储在Cookie中并进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将用户名存储在cookie中,并在下次用户打开网站时检索它。是否可以创建一个在浏览器关闭时不会过期的cookie。我正在使用asp.net c#创建网站。以及如何阻止浏览器提供保存用户名和密码的功能

I want to store the username in the cookie and retrieve it the next time when the user opens the website. Is it possible to create a cookie which doesnt expires when the browser is closed. I am using asp.net c# to create the website. And how can I stop the browser from offering to save username and password

推荐答案

编写cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");

读取cookie

HttpCookie 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");

这篇关于如何将字符串存储在Cookie中并进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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