如何永久存储cookie

How to store cookie permanently

我不想在我的应用程序代码中显示邮件 ID。我想给文本框和我给它的任何电子邮件 ID 应该永远存储在 web.config 文件中,直到我更改它。

string store= "kumar@gmail.com";
ConfigurationManager.AppSettings["MailId"] = store;
string message1 = ConfigurationManager.AppSettings["MailId"];

<appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="2001"/>
    <add key="MailId" value="krishnamohan.p@sun.com" />
</appSettings>

伪代码:

添加 cookie 的代码

HttpCookie e = new HttpCookie("d");
e.Value = "set-Email-Id";
e.Expires = DateTime.Now.AddDays(30); // expires after 30 days
HttpContext.Current.Response.Cookies.Add(e);

通过名称读取(获取)cookie 的代码

HttpCookie ck_d = Request.Cookies["d"];
 if(ck_d!=null)
 {
     // logic here
 }
string MailID = ConfigurationManager.AppSettings["MailId"];

创建一个 cookie

HttpCookie mailCookie= new HttpCookie("mailCookie");

在 cookie 中添加键值

mailCookie.Values.Add("MailID", MailID);

设置 cookie 到期日期时间。保持最大值。

mailCookie.Expires = DateTime.MaxValue;

最重要的是,将cookie写入客户端。

Response.Cookies.Add(mailCookie);

从请求中读取 cookie。

HttpCookie mailCookie= Request.Cookies["mailCookie"];
if (mailCookie== null)
{
    //No cookie found or cookie expired.
}

找到 Cookie。

if (!string.IsNullOrEmpty(mailCookie.Values["MailID"]))
{
    string MailID= mailCookie.Values["MailID"].ToString();
}
        HttpCookie Cookie = new HttpCookie("cksunlightitmailid");
        Cookie.Value = txtSunlightitmailid.Text.Trim();
        Cookie.Expires = DateTime.MaxValue; // never expire
        HttpContext.Current.Response.Cookies.Add(Cookie);
        HttpCookie ck_d = Request.Cookies["cksunlightitmailid"];
if (Request.Cookies["cksunlightitmailid"] != null)
        {
            lblSunlightitmailid.Text = "Ur current email id :" + Request.Cookies["cksunlightitmailid"].Value;
            //Or Write ur own code here
        }