/// <summary> /// 处理request中的session信息 /// </summary> /// <param name="p"></param> /// <param name="d"></param> protected virtual void ProcessRequestSession(TParameter p, TData d) { var context = p.CurrentHttpContext; try { if (context.Session == null) { return; } } catch (Exception ex) { GlobalCommon.Logger.WriteLog(LoggerLevel.WARN, "当前session无法使用,不做session处理,错误信息:" + ex.Message); return; } var sessionid = ComFunc.nvl(context.Request.Cookies["ASP.NET_SessionId"]); lock (lockobj) { //websocket下无session //因websocket无session需要将cache中的数据域与session中同步一下 if (sessionid != "") { if (GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid) != null) { var sessionobj = (FrameDLRObject)GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); foreach (var key in sessionobj.Keys) { context.Session.SetObject(key, sessionobj.GetValue(key)); } var sessionkeys = context.Session.Keys.Cast <string>().ToArray();; var removekeys = new List <string>(); foreach (string s in sessionkeys) { if (sessionobj.GetValue(s) == null) { removekeys.Add(s); } } foreach (var s in removekeys) { context.Session.Remove(s); } } var skeys = context.Session.Keys.Cast <string>().ToArray(); foreach (string s in skeys) { //深度复制session中的对象,防止p在最后释放时导致session对象丢失 p[DomainKey.SESSION, s] = ComFunc.CloneObject(context.Session.GetObject(s)); } p[DomainKey.SESSION, "SessionID"] = sessionid; } } }
/// <summary> /// 处理request中的session信息 /// </summary> /// <param name="p"></param> /// <param name="d"></param> protected virtual void ProcessRequestSession(TParameter p, TData d) { var sessionid = ComFunc.nvl(p.CurrentHttpContext.Request.Cookies["ASP.NET_SessionId"]); if (sessionid != "" && GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid) != null) { var sessionobj = (FrameDLRObject)GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); foreach (var key in sessionobj.Keys) { //深度复制session中的对象,防止p在最后释放时导致session对象丢失 p[DomainKey.SESSION, key] = ComFunc.CloneObject(sessionobj.GetValue(key)); } } p[DomainKey.SESSION, "SessionID"] = sessionid; }
/// <summary> /// 处理Response的session数据 /// </summary> /// <param name="p"></param> /// <param name="d"></param> protected virtual void ProcessResponseSeesion(TParameter p, TData d) { lock (lockobj) { var sessionid = ComFunc.nvl(CurrentContext.Request.Cookies["ASP.NET_SessionId"]); if (p.IsNeedSessionAbandon) { CurrentContext.Session.Clear(); GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); } else { IEnumerable <string> keys = p.Domain(DomainKey.SESSION).Keys; //Websocket下无session,将session对象写入ApplicationCache中用于同步 if (!string.IsNullOrEmpty(sessionid)) { var removeobj = GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); if (removeobj != null) { ((FrameDLRObject)removeobj).Dispose(); } GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); FrameDLRObject sessionobj = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); foreach (string s in keys) { sessionobj.SetValue(s, ComFunc.CloneObject(p[DomainKey.SESSION, s])); } GlobalCommon.ApplicationCache.Set("__frame_session__" + sessionid, sessionobj, TimeSpan.FromMinutes(20)); } //websocket下没有session if (CurrentContext.Session != null) { foreach (string s in keys) { CurrentContext.Session.SetObject(s, ComFunc.CloneObject(p[DomainKey.SESSION, s])); } } } } }
/// <summary> /// 处理Response的session数据 /// </summary> /// <param name="p"></param> /// <param name="d"></param> protected virtual void ProcessResponseSeesion(TParameter p, TData d) { try { if (CurrentContext.Session == null) { return; } } catch (Exception ex) { GlobalCommon.Logger.WriteLog(LoggerLevel.WARN, "当前session无法使用,不做session处理,错误信息:" + ex.Message); return; } lock (lockobj) { var sessionid = ComFunc.nvl(CurrentContext.Request.Cookies["ASP.NET_SessionId"]); if (p.IsNeedSessionAbandon) { CurrentContext.Session.Clear(); GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); } else { IEnumerable <string> keys = p.Domain(DomainKey.SESSION).Keys; //Websocket下无session,将session对象写入ApplicationCache中用于同步 if (!string.IsNullOrEmpty(sessionid)) { var removeobj = GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); if (removeobj != null) { ((FrameDLRObject)removeobj).Dispose(); } GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); FrameDLRObject sessionobj = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); foreach (string s in keys) { sessionobj.SetValue(s, ComFunc.CloneObject(p[DomainKey.SESSION, s])); } GlobalCommon.ApplicationCache.Set("__frame_session__" + sessionid, sessionobj, TimeSpan.FromMinutes(20)); //websocket下没有session if (CurrentContext.Session != null) { foreach (string s in keys) { CurrentContext.Session.SetObject(s, ComFunc.CloneObject(p[DomainKey.SESSION, s])); } } if (!IsWebSocket) { keys = CurrentContext.Session.Keys.Cast <string>().Except(keys).ToArray(); foreach (string s in keys) { CurrentContext.Session.Remove(s); } } } } } }
protected virtual void AfterProcess(HttpContext context, WP p, WD d) { lock (lockobj) { var sessionid = context.Request.Params["ASP.NET_SessionId"]; if (p.IsNeedSessionAbandon) { context.Session.Abandon(); GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); } else { IEnumerable <string> keys = p.Domain(DomainKey.SESSION).Keys; //Websocket下无session,将session对象写入ApplicationCache中用于同步 if (!string.IsNullOrEmpty(sessionid)) { var removeobj = GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); if (removeobj != null) { ((FrameDLRObject)removeobj).Dispose(); } GlobalCommon.ApplicationCache.Remove("__frame_session__" + sessionid); FrameDLRObject sessionobj = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); foreach (string s in keys) { sessionobj.SetValue(s, ComFunc.CloneObject(p[DomainKey.SESSION, s])); } GlobalCommon.ApplicationCache.Set("__frame_session__" + sessionid, sessionobj, TimeSpan.FromMinutes(20)); } //websocket下没有session if (context.Session != null) { foreach (string s in keys) { context.Session[s] = ComFunc.CloneObject(p[DomainKey.SESSION, s]); } } if (!IsWebSocket) { keys = context.Session.Keys.Cast <string>().Except(keys).ToArray(); foreach (string s in keys) { context.Session.Remove(s); } } keys = p.Domain(DomainKey.APPLICATION).Keys; foreach (string s in keys) { context.Application[s] = ComFunc.CloneObject(p[DomainKey.APPLICATION, s]); } keys = context.Application.Keys.Cast <string>().Except(keys).ToArray(); foreach (string s in keys) { context.Application.Remove(s); } } } //处理cookie var cookies = (FrameDLRObject)p.ExtentionObj.cookie; var addcookies = p.ExtentionObj.cookie.add == null ? (FrameDLRObject)FrameDLRObject.CreateInstance() : (FrameDLRObject)p.ExtentionObj.cookie.add; var removecookies = p.ExtentionObj.cookie.remove == null ? (FrameDLRObject)FrameDLRObject.CreateInstance() : (FrameDLRObject)p.ExtentionObj.cookie.remove; foreach (var key in addcookies.Keys) { var item = (FrameDLRObject)addcookies.GetValue(key); var name = ComFunc.nvl(item.GetValue("name")); var value = ComFunc.nvl(item.GetValue("value")); var domain = ComFunc.nvl(item.GetValue("domain")); var expire = (DateTime)item.GetValue("expire"); HttpCookie hc = new HttpCookie(name); hc.Value = value; hc.Domain = domain; if (expire != DateTime.MinValue) { hc.Expires = expire; } HttpContext.Current.Response.Cookies.Add(hc); } foreach (var key in removecookies.Keys) { var item = (FrameDLRObject)removecookies.GetValue(key); var name = ComFunc.nvl(item.GetValue("name")); HttpCookie currentUserCookie = HttpContext.Current.Request.Cookies[name]; if (currentUserCookie != null) { HttpContext.Current.Response.Cookies.Remove(name); currentUserCookie.Expires = DateTime.Now.AddDays(-10); currentUserCookie.Value = null; HttpContext.Current.Response.SetCookie(currentUserCookie); } } }
public static void Prepare(HttpContext context, ref WP p, ref WD d) { var url = context.Request.Path.Replace("\\", "/"); if (url == "" && url == "/") { url = GlobalCommon.WebCommon.StartPage; } p.RequestResourcePath = ComFunc.nvl(url); var ext = Path.GetExtension(p.RequestResourcePath); if (ext != "") { string reqpath = Path.GetFileNameWithoutExtension(p.RequestResourcePath); string[] ss = reqpath.Split('.'); p.Action = ss.Length > 1 ? ss[1] : ""; p.RequestResourceName = ss.Length > 0 ? ss[0] : ""; p.RequestResources = ss; } else { var turl = p.RequestResourcePath.Replace("~", ""); turl = turl.StartsWith("/") ? turl.Substring(1) : turl; string[] ss = turl.Split('/'); p.Action = ss.Length > 1 ? ss[1] : ""; p.RequestResourceName = ss.Length > 0 ? ss[0] : ""; p.RequestResources = ss; } foreach (string s in context.Request.QueryString.Keys) { p[DomainKey.QUERY_STRING, s] = context.Request.QueryString[s]; } foreach (string s in context.Request.Form.AllKeys) { p[DomainKey.POST_DATA, s] = context.Request.Form[s]; } //获取上传文件的二进制流 foreach (string s in context.Request.Files.AllKeys) { p[DomainKey.UPDATE_FILE, s] = context.Request.Files[s]; } lock (lockobj) { //websocket下无session if (!context.IsWebSocketRequest) { //因websocket无session需要将cache中的数据域与session中同步一下 var sessionid = context.Session.SessionID; if (sessionid != "" && GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid) != null) { var sessionobj = (FrameDLRObject)GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); foreach (var key in sessionobj.Keys) { context.Session[key] = sessionobj.GetValue(key); } var sessionkeys = context.Session.Keys.Cast <string>().ToArray();; var removekeys = new List <string>(); foreach (string s in sessionkeys) { if (sessionobj.GetValue(s) == null) { removekeys.Add(s); } } foreach (var s in removekeys) { context.Session.Remove(s); } } var skeys = context.Session.Keys.Cast <string>().ToArray(); foreach (string s in skeys) { //深度复制session中的对象,防止p在最后释放时导致session对象丢失 p[DomainKey.SESSION, s] = ComFunc.CloneObject(context.Session[s]); } } else { var sessionid = context.Request.Params["ASP.NET_SessionId"]; if (sessionid != "" && GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid) != null) { var sessionobj = (FrameDLRObject)GlobalCommon.ApplicationCache.Get("__frame_session__" + sessionid); foreach (var key in sessionobj.Keys) { //深度复制session中的对象,防止p在最后释放时导致session对象丢失 p[DomainKey.SESSION, key] = ComFunc.CloneObject(sessionobj.GetValue(key)); } } } foreach (var s in context.Application.Keys) { //深度复制application中的对象,防止p在最后释放时导致application对象丢失 p[DomainKey.APPLICATION, s.ToString()] = ComFunc.CloneObject(context.Application[s.ToString()]); } } if (context.Request.ContentType.ToLower().IndexOf("/json") > 0) { var sr = new StreamReader(context.Request.InputStream); var str = sr.ReadToEnd(); if (!string.IsNullOrEmpty(str)) { FrameDLRObject o = null; FrameDLRObject.TryParse(str, FrameDLRFlags.SensitiveCase, out o); if (o != null) { foreach (var k in o.Keys) { p[DomainKey.POST_DATA, k] = o.GetValue(k); } } } p.RequestContent = str; } //xml if (context.Request.ContentType.ToLower().IndexOf("/xml") > 0) { var sr = new StreamReader(context.Request.InputStream); var str = sr.ReadToEnd(); if (!string.IsNullOrEmpty(str)) { FrameDLRObject o = FrameDLRObject.CreateInstance(str, FrameDLRFlags.SensitiveCase); if (o != null) { foreach (var k in o.Keys) { p[DomainKey.POST_DATA, k] = o.GetValue(k); } } } p.RequestContent = str; } //multipart/form-data if (context.Request.ContentType.ToLower().IndexOf("multipart/form-data") >= 0) { var mp = ParseMultipartFormData(context.Request.InputStream, context.Request.ContentEncoding); foreach (var k in mp.Keys) { if (mp.GetValue(k) is FrameUploadFile) { p[DomainKey.UPDATE_FILE, k] = mp.GetValue(k); } else { p[DomainKey.POST_DATA, k] = mp.GetValue(k); } } } p.ExtentionObj.cookie = FrameDLRObject.CreateInstance(); if (context.Request.Cookies != null && context.Request.Cookies.Count > 0) { foreach (var key in context.Request.Cookies.AllKeys) { if (key != "ASP.NET_SessionId") { FrameDLRObject item = FrameDLRObject.CreateInstance(); item.SetValue("name", context.Request.Cookies[key].Name); item.SetValue("value", context.Request.Cookies[key].Value); item.SetValue("domain", context.Request.Cookies[key].Domain); item.SetValue("expire", context.Request.Cookies[key].Expires); ((FrameDLRObject)p.ExtentionObj.cookie).SetValue(ComFunc.nvl(item.GetValue("name")), item); } } } p[DomainKey.SESSION, "SessionID"] = context.IsWebSocketRequest ? context.Request.Params["ASP.NET_SessionId"] : context.Session.SessionID; p.SetValue(DomainKey.APPLICATION_ENVIRONMENT, "ServerRootPath", context.Server.MapPath("~")); p.SetValue(DomainKey.APPLICATION_ENVIRONMENT, "ServerRootPath_URL", context.Request.Url.AbsoluteUri.Replace(context.Request.FilePath, "")); p.SetValue(DomainKey.APPLICATION_ENVIRONMENT, "WebPath", context.Request.Url.AbsoluteUri.Replace(context.Request.RawUrl, "")); p.SetValue(DomainKey.APPLICATION_ENVIRONMENT, "Domain", context.Request.Url.Host); }