2010-05-10 40 views
19

通过静态对象的静态属性访问asp.net会话变量是否安全?通过静态对象的静态属性访问asp.net会话变量是否安全?

这里是我的意思是:

public static class SessionHelper 
{ 
    public static int Age 
    { 
     get 
     { 
      return (int)HttpContext.Current.Session["Age"]; 
     } 

     set 
     { 
      HttpContext.Current.Session["Age"] = value; 
     } 
    } 


    public static string Name 
    { 
     get 
     { 
      return (string)HttpContext.Current.Session["Name"]; 
     } 

     set 
     { 
      HttpContext.Current.Session["Name"] = value; 
     } 
    } 
} 

有没有可能是用户A可以访问用户B的会话数据的这种方式?

+1

HttpContext.Current从Thread.CurrentThread获取上下文,以防万一您想知道它是如何工作的。 – Greg 2010-05-10 16:17:18

回答

30

是的,这样是好的 - 只要确保你做到这一点:

public static class SessionHelper 
{ 

    private static HttpSession sess = HttpContext.Current.Session; 
    public static int Age 
    { 
     get 
     { 
      return (int)sess["Age"]; 
     } 

     set 
     { 
      sess["Age"] = value; 
     } 
    } 
} 

正如我见过这样一个展示用户的会话数据给其他用户。 (虽然在ASP.NET 1.1)

+1

+1这是一个等待发生的巨大痛苦。 – Greg 2010-05-10 16:16:36

+1

我在IIS 6.0上运行ASP.NET MVC 3(.NET 4.0)时经历了巨大的痛苦。交叉污染只发生在重负荷下,因此很难诊断。显然它甚至在更高版本中仍然会发生。 – 2013-10-16 16:26:31

+0

为什么会发生这种情况?静态HttpSession变量是否在会话之间泄漏,因为它和类是静态的(因此实例可能会在会话之间重用)? – benedikt 2014-11-06 12:22:28

8

恕我直言,这实际上是一个很好的方法。它是类型安全的,增加了级别抽象,可以让你以最小的影响改变事物。

一个你可能会改变的例子,如果你决定某个状态应该移动到缓存中,或者甚至是与缓存结合的数据库,这些都需要额外的线程同步,但都可以由这个类的内部处理。您可能会考虑将班级名称更改为特定于会话的内容。

我对你的特例有一点意见,那就是你应该检查Session变量是不是null,并且返回一个适当的默认值,断言或者如果是的话就引发一个信息异常。以防万一属性在被设置之前被读取。

0

其实,这里是我的“基础”SessionClass。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

public static class CSession 
{ 
    private static readonly string zE = ""; 
    private static readonly string CrLF = Environment.NewLine; 
    private static bool bStopHere = true; 

    /// <summary> 
    /// Get a session variable 
    /// </summary> 
    /// <param name="pSessionKey"></param> 
    /// <returns></returns> 
    public static object Get(string pSessionKey) 
    { 
     object t = null; 
     if (HttpContext.Current.Session[pSessionKey] != null) { t = (object)HttpContext.Current.Session[pSessionKey]; } 
     return t; 
    }//object Get(string pSessionKey) 



    /// <summary> 
    /// Set a session variable 
    /// </summary> 
    /// <param name="pSessionKey"></param> 
    /// <param name="pObject"></param> 
    public static void Set(string pSessKey, object pObject) 
    { 
     HttpContext.Current.Session.Remove(pSessKey); 
     HttpContext.Current.Session.Add(pSessKey, pObject); 
    }//void Set(string pSessionKey, object pObject) 


    public static string GetString(string pSessKey) 
    { 
     string sTemp = zE; 
     object t = Get(pSessKey); 
     if (t != null) { sTemp = (string)t; } else { sTemp = zE; } 
     return sTemp; 
    }//string GetString(string pSessionKey) 


    public static int GetInt(string pSessKey) 
    { 
     int s = 0; 
     object t = Get(pSessKey); 
     if (t != null) { s = (int)t; } 
     return s; 
    }//int GetInt(string pSessionKey) 


    public static Int32 GetInt32(string pSessKey) 
    { 
     Int32 s = 0; 
     object t = Get(pSessKey); 
     if (t != null) { s = (Int32)t; } 
     return s; 
    }//Int32 GetInt32(string pSessionKey) 


    public static bool GetBool(string pSessKey) 
    { 
     bool s = false; 
     object t = Get(pSessKey); 
     if (t != null) { s = (bool)t; } 
     return s; 
    }//bool GetBool(string pSessionKey) 

}//static class CSession