SessionHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Web;
  2. namespace Ant.Service.Common
  3. {
  4. /// <summary>
  5. /// Session 操作类
  6. /// 1、GetSession(string name)根据session名获取session对象
  7. /// 2、SetSession(string name, object val)设置session
  8. /// </summary>
  9. public class SessionHelper
  10. {
  11. /// <summary>
  12. /// 根据session名获取session对象
  13. /// </summary>
  14. /// <param name="name"></param>
  15. /// <returns></returns>
  16. public static object GetSession(string name)
  17. {
  18. return HttpContext.Current.Session[name];
  19. }
  20. /// <summary>
  21. /// 设置session
  22. /// </summary>
  23. /// <param name="name">session 名</param>
  24. /// <param name="val">session 值</param>
  25. public static void SetSession(string name, object val)
  26. {
  27. HttpContext.Current.Session.Remove(name);
  28. HttpContext.Current.Session.Add(name, val);
  29. }
  30. /// <summary>
  31. /// 添加Session,调动有效期为20分钟
  32. /// </summary>
  33. /// <param name="strSessionName">Session对象名称</param>
  34. /// <param name="strValue">Session值</param>
  35. public static void Add(string strSessionName, string strValue)
  36. {
  37. HttpContext.Current.Session[strSessionName] = strValue;
  38. HttpContext.Current.Session.Timeout = 20;
  39. }
  40. /// <summary>
  41. /// 添加Session,调动有效期为20分钟
  42. /// </summary>
  43. /// <param name="strSessionName">Session对象名称</param>
  44. /// <param name="strValues">Session值数组</param>
  45. public static void Adds(string strSessionName, string[] strValues)
  46. {
  47. HttpContext.Current.Session[strSessionName] = strValues;
  48. HttpContext.Current.Session.Timeout = 20;
  49. }
  50. /// <summary>
  51. /// 添加Session
  52. /// </summary>
  53. /// <param name="strSessionName">Session对象名称</param>
  54. /// <param name="strValue">Session值</param>
  55. /// <param name="iExpires">调动有效期(分钟)</param>
  56. public static void Add(string strSessionName, string strValue, int iExpires)
  57. {
  58. HttpContext.Current.Session[strSessionName] = strValue;
  59. HttpContext.Current.Session.Timeout = iExpires;
  60. }
  61. /// <summary>
  62. /// 添加Session
  63. /// </summary>
  64. /// <param name="strSessionName">Session对象名称</param>
  65. /// <param name="strValues">Session值数组</param>
  66. /// <param name="iExpires">调动有效期(分钟)</param>
  67. public static void Adds(string strSessionName, string[] strValues, int iExpires)
  68. {
  69. HttpContext.Current.Session[strSessionName] = strValues;
  70. HttpContext.Current.Session.Timeout = iExpires;
  71. }
  72. /// <summary>
  73. /// 读取某个Session对象值
  74. /// </summary>
  75. /// <param name="strSessionName">Session对象名称</param>
  76. /// <returns>Session对象值</returns>
  77. public static string Get(string strSessionName)
  78. {
  79. if (HttpContext.Current.Session[strSessionName] == null)
  80. {
  81. return null;
  82. }
  83. else
  84. {
  85. return HttpContext.Current.Session[strSessionName].ToString();
  86. }
  87. }
  88. /// <summary>
  89. /// 读取某个Session对象值数组
  90. /// </summary>
  91. /// <param name="strSessionName">Session对象名称</param>
  92. /// <returns>Session对象值数组</returns>
  93. public static string[] Gets(string strSessionName)
  94. {
  95. if (HttpContext.Current.Session[strSessionName] == null)
  96. {
  97. return null;
  98. }
  99. else
  100. {
  101. return (string[])HttpContext.Current.Session[strSessionName];
  102. }
  103. }
  104. /// <summary>
  105. /// 删除某个Session对象
  106. /// </summary>
  107. /// <param name="strSessionName">Session对象名称</param>
  108. public static void Del(string strSessionName)
  109. {
  110. HttpContext.Current.Session[strSessionName] = null;
  111. }
  112. /// <summary>
  113. /// 移除Session
  114. /// </summary>
  115. public static void Remove(string sessionname)
  116. {
  117. if (HttpContext.Current.Session[sessionname] != null)
  118. {
  119. HttpContext.Current.Session.Remove(sessionname);
  120. HttpContext.Current.Session[sessionname] = null;
  121. }
  122. }
  123. }
  124. }