62,046
社区成员
发帖
与我相关
我的任务
分享
<httpHandlers>
<clear />
</httpHandlers>
<httpModules>
<clear />
<add name="HttpModule" type="Xy.Web.HttpModule,Xy.Web" />
</httpModules>
HttpApplication.Context.Session
HttpContext.Current.Session
public class PageSessionCollection : IPageSessionCollection {
private static Dictionary<string, IPageSession> _sessionCollection;
private static PageSessionCollection _instance;
private PageSessionCollection() {
_sessionCollection = new Dictionary<string, IPageSession>();
}
public static PageSessionCollection GetInstance() {
if (_instance == null) {
_instance = new PageSessionCollection();
}
return _instance;
}
public int CreateSession() {
string Key = Guid.NewGuid().ToString();
System.Web.HttpContext hcr = System.Web.HttpContext.Current;
hcr.Response.Cookies.Add(new System.Web.HttpCookie("XyFrameSession", Key) { Expires = DateTime.Now.AddMinutes(Xy.WebSetting.SessionOutTime) });
_sessionCollection.Add(Key, new PageSession());
return _sessionCollection.Count;
}
public IPageSession GetSession() {
string Key;
System.Web.HttpContext hcr = System.Web.HttpContext.Current;
if (hcr.Request.Cookies["XyFrameSessionId"] == null) {
CreateSession();
}
Key = hcr.Request.Cookies["XyFrameSessionId"].Value;
return _sessionCollection[Key];
}
}
public class PageSession : IPageSession {
private System.Collections.Specialized.NameValueCollection _session;
public PageSession() {
_session = new System.Collections.Specialized.NameValueCollection();
}
public string this[string index] {
get {
return _session[index];
}
set {
if (_session[index] == null) {
_session.Add(index, value);
}
}
}
}
public interface IPageSessionCollection {
int CreateSession();
IPageSession GetSession();
}
public interface IPageSession {
string this[string index] { get; set; }
}
public class SessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += BeginTransaction;
context.EndRequest += CommitAndCloseSession;
context.PreRequestHandlerExecute += PreRequestHandlerExecute;
}
public void Dispose() { }
public void PreRequestHandlerExecute(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
context.Session["some_sesion"] = new SomeObject();
}
...
}