如何在 EnableSessionState=“False”的请求上获取 SessionID

发布于 2024-08-03 09:35:08 字数 438 浏览 3 评论 0原文

我希望能够在 WebMethod 函数中获取当前经过身份验证的会话的 SessionID,其中 EnableSession = false

我无法在此请求上设置EnableSession=true,因为不同页面上的另一个(长时间运行的)请求正在保持SessionState锁定(EnableSessionState ==“True”而不是“Readonly”) ”)。

是否有一致的方法从 ASP.NET 会话 cookie 或无 cookie 会话的 Url 获取 SessionID?我可以自己编写代码,但我宁愿使用已经记录和测试的函数。

非常感谢,
弗罗林.

I want to be able to get the SessionID of the currently authenticated session in a WebMethod function where EnableSession = false.

I cannot set EnableSession=true on this request, because another (long running) request on a different page is keeping the SessionState locked (EnableSessionState == "True" not "Readonly").

Is there a consistent way of getting the SessionID from either the ASP.NET Session cookie or the Url for cookieless sessions? I can code it myself but I would rather use a function that is already documented and tested.

Thank you very much,
Florin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-08-10 09:35:08

似乎没有 ASP.NET 函数可以做到这一点,所以我自己编写了一个 hack,它暂时有效……;):

private string GetSessionID(HttpContext context)
{
  var cookieless = context.Request.Params["HTTP_ASPFILTERSESSIONID"];
  if (!string.IsNullOrEmpty(cookieless))
  {
    int start = cookieless.LastIndexOf("(");
    int finish = cookieless.IndexOf(")");
    if (start != -1 && finish != -1)
      return cookieless.Substring(start + 1, finish - start - 1);
  }
  var cookie = context.Request.Cookies["ASP.NET_SessionId"];
  if (cookie != null)
    return cookie.Value;
  return null;
}

There seems to be no ASP.NET function that can do this, so I made up a hack of my own, which works... for now ;):

private string GetSessionID(HttpContext context)
{
  var cookieless = context.Request.Params["HTTP_ASPFILTERSESSIONID"];
  if (!string.IsNullOrEmpty(cookieless))
  {
    int start = cookieless.LastIndexOf("(");
    int finish = cookieless.IndexOf(")");
    if (start != -1 && finish != -1)
      return cookieless.Substring(start + 1, finish - start - 1);
  }
  var cookie = context.Request.Cookies["ASP.NET_SessionId"];
  if (cookie != null)
    return cookie.Value;
  return null;
}
只有影子陪我不离不弃 2024-08-10 09:35:08

HttpContext.Current.Session.SessionID

HttpContext.Current.Session.SessionID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文