问题
I have an issue with GenericHandler and anonymousIdentification.
Basically if <anonymousIdentification enabled="true" />
is turned on in the web config, whenever a JQuery GET/POST request is sent to the server, that request executes under a new user and a new user session.
Is there a way to mitigate this? I need to access the current user's session variables... It is really frustrating!
回答1:
Generic handlers must implement the IReadOnlySessionState interface to access session variables. If you also need to write session variables, implement IRequiresSessionState.
回答2:
Implement the System.Web.SessionState.IRequiresSessionState
interface:
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Session["StackOverflow"] = "overflowing";
context.Response.Redirect("~/AnotherPage.aspx");
}
}
回答3:
You can use this:
public class Handler :
IHttpHandler,
System.Web.SessionState.IReadOnlySessionState
回答4:
I suggest in the web browser you enable the network tab in the developer mode. Then check in the AJAX which cookie is sent (if any).
If you do not see any cookie, it means the browser did not get any session, thus you should make sure (as stated by Josh) to inherit the right interface and access the cookie. (Don'f forget to use System.Web.SessionState)
In order to generate the cookie, access the session object's sessionID:
string sesId = context.Session.SessionID;
Before you write anything to the response.
来源:https://stackoverflow.com/questions/4889437/asp-net-generic-handlers-session