How to get SessionID on a request where EnableSessionState=“False”

孤者浪人 提交于 2019-12-10 07:09:24

问题


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.


回答1:


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;
}



回答2:


HttpContext.Current.Session.SessionID



来源:https://stackoverflow.com/questions/1378352/how-to-get-sessionid-on-a-request-where-enablesessionstate-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!