【问题标题】:How to get SessionID on a request where EnableSessionState="False"如何在 EnableSessionState="False" 的请求中获取 SessionID
【发布时间】:2010-11-25 14:06:43
【问题描述】:

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

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

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

非常感谢,
弗洛林。

【问题讨论】:

    标签: asp.net session-state webmethod sessionid


    【解决方案1】:

    似乎没有 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;
    }
    

    【讨论】:

      【解决方案2】:

      HttpContext.Current.Session.SessionID

      【讨论】:

      • Session 在这个请求中不可用,所以 HttpContext.Current.Session == null。
      猜你喜欢
      • 2013-05-30
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2013-04-10
      相关资源
      最近更新 更多