How to use cookies and sessions in detail

1. Single-level Cookie

1. Create:
Method 1:

 HttpCookie cookie = new HttpCookie("UserId"); 
 cookie.Value = "蝈蝈";
 //HttpCookie cookie = new HttpCookie("UserId", "蝈蝈");    
 cookie.Expires =DateTime.Now.AddMinutes(2);
 HttpContext.Current.Response.AppendCookie(cookie);
 //HttpContext.Current.Response.Cookies.Add(cookie);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Method Two:

HttpContext.Current.Response.Cookies["Pwd"].Value = "123456";
HttpContext.Current.Response.Cookies["Pwd"].Expires = DateTime.Now.AddMinutes(2); 
  • 1
  • 2

2. Read

if (HttpContext.Current.Request.Cookies[key] != null)
 {
    string value = HttpContext.Current.Request.Cookies[key];
}
else
{
   string value = "不存在键:" + key;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3. Modify

if (HttpContext.Current.Request.Cookies[key] != null)
 {
     HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
     cookie.Value = value; //必须判空
     HttpContext.Current.Response.Cookies.Add(cookie);
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. Delete

HttpContext.Current.Request.Cookies.Remove("Pwd");
  • 1

After deletion, HttpContext.Current.Request.Cookies["Pwd"] can still be read, and setting the Expires property to a certain time in the past can have the effect of deletion.
5. Modify the expiration time

if (HttpContext.Current.Request.Cookies[key] != null)
 {
     HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
     cookie.Expires = DateTime.Now.AddMinutes(time);
     HttpContext.Current.Response.Cookies.Add(cookie);
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. Multi-level Cookies

1. Create:

HttpCookie cookie = new HttpCookie("Guo", "MainCookieValue");
//HttpCookie cookie = new HttpCookie("Guo");
//cookie.Value = "MainCookieValue";
cookie.Expires = DateTime.Now.AddMinutes(2);
cookie.Values["Age"] = "18";
cookie.Values["Name"] = "蝈蝈";
cookie.Values.Add("Phone", "18233399999");
HttpContext.Current.Response.Cookies.Add(cookie);
//string value = HttpContext.Current.Request.Cookies["Guo"].Value;
//value == MainCookieValue&Age=18&Name=蝈蝈&Phone=18233399999
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2. Read

if (HttpContext.Current.Request.Cookies[key] != null)
{
    return HttpContext.Current.Request.Cookies[key][subKey] ?? 
    "不存在键:" + key + "->" + subKey;
}
else
{
    return "不存在键:" + key;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3. Modify

if (HttpContext.Current.Request.Cookies[key] != null)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
    cookie[subKey] = value; //存在修改,不存在添加
    HttpContext.Current.Response.Cookies.Add(cookie);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. Delete

HttpContext.Current.Request.Cookies["Guo"].Values.Remove("Age"); 
  • 1

After deleting, HttpContext.Current.Request.Cookies["Guo"] ["Age"] or HttpContext.Current.Request.Cookies["Guo"].Values["Age"]
can still be read, set the Expires property to the past A certain time can have the effect of deletion

3. Session

1. Create:

HttpContext.Current.Session["UserID"] = "abc";
HttpContext.Current.Session["Pwd"] = "123";
  • 1
  • 2

2. Read

string result = "Session[" + name + "]不存在¨²";
if (HttpContext.Current.Session[name] != null)
{
    result = HttpContext.Current.Session[name].ToString();
}
  • 1
  • 2
  • 3
  • 4
  • 5

3. Modify

string msg = string.Empty;
if (HttpContext.Current.Session[key] != null)
{
    HttpContext.Current.Session[key] = value; //存在修改
}
else
{
    HttpContext.Current.Session[key] = value; //不存在添加
    msg = "Session[" + key + "]==null";
}
return msg;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4. Delete

HttpContext.Current.Session.Remove(key);
  • 1

Session deletion does have an effect, and it cannot be read after deletion, but the session cannot be expired immediately by setting TimeOut, because the value of TimeOut must be an integer greater than 0.
5. Set the expiration time. The
page -level TimeOut priority is higher than that in Web.Config set timeout

Session.Timeout = 3;
<sessionState mode="InProc" timeout="1"></sessionState>
  • 1
  • 2

To use Session in the ashx file, you need to refer to using System.Web.SessionState;, and then implement the IrequiresSessionState interface,
otherwise it will always report "Error: Session=null" when executing context.Session.TimeOut=1;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479923&siteId=291194637