静态类与Cookie保存全局信息的应用
1.在编程过程中,我们可能遇到涉及用户登录的一些应用,在登录页面,用户只需要输入用户名和密码即可,但是在登录到主页面中,我们可能要经常用到不止是用户名信息,有可能还有性别、序号等等信息,但是在实际操作过程我们不可能每次想用性别、序号等信息就通过用户名再获取一次数据吧?这显然不可能,在这里我们可以考虑用cookie及静态类保存这些全局的基本信息。
用户登录时,用户信息加载到cookie中
string value1 = dt.Rows[0]["USERID"].ToString() + "#" + dt.Rows[0]["SSDW"].ToString() + "#" + dt.Rows[0]["DWBH"].ToString() + "#" + dt.Rows[0]["XM"].ToString() + "#" + dt.Rows[0]["JSMC"].ToString() + "####"; HttpCookie passportCookie = new HttpCookie(ConfigurationManager.AppSettings["CookieName"], Server.UrlEncode(value1)); passportCookie.Path = "/"; Response.Cookies.Set(passportCookie);
其实cookie已经是全局的值了,但是在具体应用不是很方便,所以考虑将它转换为静态类。利于操作
public class PassportManager { /// <summary> /// 验证当前用户释放登录 /// </summary> public static void NeedLogin() { if (HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]] == null || string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]].Value) || HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]].Expires > DateTime.Now ) { try { HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "Default.aspx"); } catch { HttpContext.Current.Response.RedirectLocation = HttpContext.Current.Request.ApplicationPath + "Default.aspx"; } } } /// <summary> /// 验证当前用户是否登录 /// </summary> public static bool IsLogin { get { return !(HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]] == null || string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]].Value) || HttpContext.Current.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]].Expires > DateTime.Now ); } } /// <summary> /// 获取当前登录用户 /// </summary> public static LoginUser CurrentUser { get { NeedLogin(); LoginUser user = new LoginUser(); HttpContext context = HttpContext.Current; if (context.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]] != null) { string[] value = context.Server.UrlDecode(context.Request.Cookies[ConfigurationManager.AppSettings["CookieName"]].Value).Split('#'); user.Id = value[0]; user.SSDW = value[1]; user.SSDWBH = value[2]; user.XM = value[3]; user.JSMC = value[4]; user.LX = value[5]; user.TH = value[6]; user.QY = value[7]; user.YWLX = value[8]; } return user; } } /// <summary> /// 退出登录 /// </summary> public static void Logout() { HttpCookie cookie; string cookieName; int limit = HttpContext.Current.Request.Cookies.Count; for (int i = 0; i < limit; i++) { cookieName = HttpContext.Current.Request.Cookies[i].Name; cookie = new HttpCookie(cookieName); cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } HttpContext.Current.Response.Redirect("~/Default.aspx"); } /// <summary> /// 返回登录信息json字符串 /// </summary> /// <returns></returns> public static string GetJsonStr() { LoginUser user=CurrentUser; StringBuilder result = new StringBuilder(); result.Append("[{"); result.AppendFormat("Id:'{0}',SSDW:'{1}',SSDWBH:'{2}',XM:'{3}',JSMC:'{4}',LX:'{5}',TH:'{6}',QY:'{7}',YWLX:'{8}'" , user.Id, user.SSDW, user.SSDWBH, user.XM, user.JSMC, user.LX, user.TH,user.QY,user.YWLX); result.Append("}]"); return result.ToString(); } } public class LoginUser { private string _iD; public string Id { get { return _iD; } set { _iD = value; } } private string _xM; public string XM { get { return _xM; } set { _xM = value; } } private string _sSDWBH; public string SSDWBH { get { return _sSDWBH; } set { _sSDWBH = value; } } private string _sSDW; public string SSDW { get { return _sSDW; } set { _sSDW = value; } } private string _jSMC; public string JSMC { get { return _jSMC; } set { _jSMC = value; } } private string _lX; public string LX { get { return _lX; } set { _lX = value; } } private string _tH; public string TH { get { return _tH; } set { _tH = value; } } private string _qy; public string QY { get { return _qy; } set { _qy = value; } } private string _ywlx; public string YWLX { get { return _ywlx; } set { _ywlx = value; } } }
注意:以上提供的类,应该只是获取数据而已
2.数据加载到内存,单例静态类
/// <summary> /// Web缓存类 /// </summary> public static class WebCache { #region 私有静态变量 /// <summary> /// 警员姓名缓存变量 /// </summary> private static DataTable dtJYXX; /// <summary> /// 资源配置缓存变量 /// </summary> private static Dictionary<string, string> dicResourse; /// <summary> /// 颜色配置实体 /// </summary> private static ColorSection colorSection; /// <summary> /// 版本配置实体 /// </summary> private static VersionSection versionSection; /// <summary> /// 打印配置实体 /// </summary> private static PrintSection printSection; #endregion #region 私有静态构造函数 /// <summary> /// 私有静态构造函数 /// </summary> static WebCache() { #region ZXGA.BLL.JYXXB bllJYXXB = new ZXGA.BLL.JYXXB(App_Init.ConnStr()); dtJYXX = bllJYXXB.GetJyxmDW("").Tables[0]; dtJYXX.DefaultView.Sort = "JYXM asc"; #endregion #region 加载资源信息 ResourseSection resourseSection=GenericSerializer.Open<ResourseSection>(HttpContext.Current.Server.MapPath("~/Configs/Resourse.config")); if (resourseSection != null && resourseSection.ResourseInfos != null) { dicResourse = new Dictionary<string, string>(); foreach (ResourseInfo t in resourseSection.ResourseInfos) { if (!dicResourse.Keys.Contains(t.Name)) { dicResourse.Add(t.Name, t.Text); } } } #endregion #region 加载颜色配置 colorSection = GenericSerializer.Open<ColorSection>(HttpContext.Current.Server.MapPath("~/Configs/Color.config")); if (colorSection == null) { colorSection = new ColorSection(); } #endregion #region 加载版本配置 versionSection = GenericSerializer.Open<VersionSection>(HttpContext.Current.Server.MapPath("~/Configs/Version.config")); if (versionSection == null) { versionSection = new VersionSection(); } #endregion #region 加载受理单打印页配置 printSection = GenericSerializer.Open<PrintSection>(HttpContext.Current.Server.MapPath("~/Configs/SLD_Print.config")); if (printSection == null) { printSection = new PrintSection(); } #endregion } #endregion #region 公用静态方法 public static string GetJYMX_Arr(string where) { dtJYXX.DefaultView.Sort = "JYXM asc"; DataRow[] drArr = dtJYXX.Select(where); string sTemp = ""; foreach (DataRow dr in drArr) { sTemp += dr["JYXM"].ToString().Trim() + "|" + dr["XMPY"].ToString().Trim() + ";"; } return sTemp; } }
现在静态缓存类也有了,那么如何让数据加载到缓存中呢?
在web前台直接进行加载:<link href="<%=ResolveClientUrl("~/Css/Default.css"+WebCache.GetVersion())%>" rel="stylesheet" type="text/css" />
该句在加载文件的时候就已经调用了静态类,实现单例静态类数据加载到内存中