为什么 Global.asax 中的 HttpContext.Current.Session 为 null?
我正在使用 VS2010 并创建了一个简单的 asp. Web 表单应用程序,使用开发服务器对其进行测试。
我尝试在会话中存储从 sql server 查询的用户数据,因为我不想在每个请求中访问数据库。我正在使用“Application_AuthenticateRequest”和“Session_Start”方法。
第一轮: 调用了 AuthenticateRequest。运行以下代码:
public static void Initialize(string login_name, bool force_refresh)
{
HttpSessionState Session = HttpContext.Current.Session;
object o = Session == null ? null : Session["EMPLOYEE_DATA"];
if (force_refresh || o == null || o.GetType() != typeof(Employee) || (o as Employee).login_name!= login_name)
{
_current = UIManager.GetEmployee(login_name);
if (Session != null)
{
Session["EMPLOYEE_DATA"] = _current;
}
}
else
{
_current = (Employee)o;
}
}
_current 变量是通过静态属性发布的私有静态字段。 在第一轮中,Session 为 null,我认为没关系,因为 Session_Start 尚未调用。 Session_Start 看起来像这样:
protected void Session_Start(object sender, EventArgs e)
{
Session["EMPLOYEE_DATA"] = EmployeeFactory.Current;
}
在下一轮中,Session_Start 当然不会被调用,但在 AuthenticateRequest 中我无法访问会话。 HttpContext.Current.Session 为 null,并且 this.Session 引用抛出 HttpException,表示“会话状态在此上下文中不可用”。
不过,我可以从任何 page_load 事件访问会话,但这是一个不好的做法,我认为我在每个 page_load 中都进行了身份验证。 知道如何访问会话吗?
谢谢你的建议,
彼得
I'm using VS2010 and created a simple asp. web forms application, using Development Server to test it.
I try to store user data - queried from sql server - in the session, since I don't want to access database in every request. I'm using the 'Application_AuthenticateRequest' and the 'Session_Start' methods.
First round:
AuthenticateRequest called. The following code ran:
public static void Initialize(string login_name, bool force_refresh)
{
HttpSessionState Session = HttpContext.Current.Session;
object o = Session == null ? null : Session["EMPLOYEE_DATA"];
if (force_refresh || o == null || o.GetType() != typeof(Employee) || (o as Employee).login_name!= login_name)
{
_current = UIManager.GetEmployee(login_name);
if (Session != null)
{
Session["EMPLOYEE_DATA"] = _current;
}
}
else
{
_current = (Employee)o;
}
}
The _current variable is a private static field published through a static property.
In the first round the Session is null, and I think it's ok because the Session_Start not called yet.
The Session_Start looks like this:
protected void Session_Start(object sender, EventArgs e)
{
Session["EMPLOYEE_DATA"] = EmployeeFactory.Current;
}
In the next round the Session_Start is not called of course but in the AuthenticateRequest I can't access to the session. The HttpContext.Current.Session is null and the this.Session reference throw a HttpException says the "Session state is not available in this context".
However I can access the Session from any of the page_load events but it's a bad practice I think that I put authentication every page_load.
Any idea how can I access to the Session?
Thanks for advice,
Péter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在 Application_AuthenticateRequest 上使用 Session,因为它当时尚未绑定。
我认为您可以使用事件 Application_AcquireRequestState。
You're not able to use Session on the Application_AuthenticateRequest becauase it's not bound at that moment.
I think you're able to use the event Application_AcquireRequestState.
尝试在 page_Load 中使用以下代码
try to use the below code in page_Load