问题描述
我希望能够通过 WebMethod 函数(其中 EnableSession = false )获得当前已认证会话的 SessionID .
I want to be able to get the SessionID of the currently authenticated session in a WebMethod function where EnableSession = false.
我无法对此请求设置 EnableSession = true ,因为另一个页面上的另一个(长时间运行的)请求将 SessionState 保持锁定状态(EnableSessionState =="True"不是只读").
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").
是否存在一致的方法来从 ASP.NET会话cookie 或 Url 获取无cookie会话的 SessionID ?我可以自己编写代码,但我希望使用已经记录和测试过的功能.
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.
推荐答案
似乎没有ASP.NET函数可以做到这一点,所以我自己编写了一个hack,目前可以正常使用;):
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;
}
这篇关于如何在EnableSessionState ="False"的请求上获取SessionID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!