1.Cookie案例
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
public class UserState
{
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="strValue">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}
return "";
}
}
2.Session案例
using BaoYong.SystemAdministration.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace BaoYong.SystemAdministration.UI.Helper
{
public class SessionUtils
{
/// <summary>
/// 会话的key
/// </summary>
private const string sessionKey = "USERINFO_SESSION_KEY";
/// <summary>
///
/// </summary>
public HttpSessionState httpSession => HttpContext.Current.Session;
/// <summary>
/// 实例化此Session类
/// </summary>
public static SessionUtils current = new SessionUtils();
/// <summary>
/// 用户信息上下文
/// </summary>
public Sys_UserNumberInfo userInfo
{
get
{
return this.httpSession[sessionKey] as Sys_UserNumberInfo;
}
set
{
this.httpSession[sessionKey] = value;
}
}
}
}