我记录从当人们登录到我的.NET 2.0的Web应用程序会话的开始时间,但我也想记录会话ID。能有人给我如何做到这一点的一些示例代码(如何访问在Global.asax中的会话ID)。

如果您需要任何额外的信息只是让我知道。

有帮助吗?

解决方案

HttpContext.Current.Session.SessionID

编辑来显示空测试:

if ((HttpContext.Current != null) && (HttpContext.Current.Session != null) {
  id = HttpContext.Current.Session.SessionID
}

其他提示

您可以得到它很简单,与HttpContext.Current.Session.SessionId正如你可能已经知道了。你需要或之后Application_AcquireRequestState前的会话状态已被加载,和会话状态时,所请求的资源实现IRequiresSessionState也只加载。你可以看到在Global.asax中这里的所有事件的列表:的 https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5771721.html 和读更多关于IRequiresSessionState这里: http://msdn.microsoft的.com / EN-US /库/ system.web.sessionstate.irequiressessionstate.aspx

写入到会话的日期时间和以下ASP.NET的识别用户的会话的sessionid在第一请求的时刻。

protected void Application_PreRequestHandlerExecute(object sender, EventArgs eventArgs) {
    var session = HttpContext.Current.Session;
    if (session != null) {
        if (session["foo"] == null) {
            session["foo"] = DateTime.Now.Ticks + "|" + session.SessionID;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top