【问题标题】:How to store cookie permanently如何永久存储cookie
【发布时间】:2015-08-24 02:35:31
【问题描述】:

我不想在我的应用程序代码中显示邮件 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>

【问题讨论】:

标签: c# asp.net session cookies web-config


【解决方案1】:
        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
        }

【讨论】:

    【解决方案2】:
    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);
    

    从 Request 中读取 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();
    }
    

    【讨论】:

    • hai 如何更改邮件ID。文本框在哪里。我如何更改 emailid@Rahul Nikate
    【解决方案3】:

    伪代码:

    添加 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);
    

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

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

    【讨论】:

    • 我想存储邮件 id 。在此如何存储mailid。你能写我的代码吗?假设我的邮件 ID“krishna@gmail.com”@satinder singh
    • @RahulNikate: 那是上面已经提到的伪代码,虽然我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2022-01-22
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多