IE7的不同Tab生成不同ID
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Net;
8
9 using TFSBussTier.objects;
10
11 namespace TFSBussTier.bussTier
12 {
13
14 /// <summary>
15 /// To Manage the session
16 /// </summary>
17 public class SessionMgr
18 {
19 // Session Identity for different page
20 public readonly string pageIdentity = "PageIdentity";
21
22 // Private instance for Session Manager
23 private static SessionMgr instance = null;
24
25 // Http context for current request
26 private HttpContext context;
27
28 //private static Hashtable excludeSessions;
29 private static string []excludeSessions;
30
31
32 public SessionMgr()
33 {
34 //excludeSessions.Add("CountryCode","CountryCode");
35 excludeSessions = new string[]{
36 "CountryCode",
37 "CTaxINI",
38 "iErrorLogID",
39 "LoginDefault",
40 "StaffID",
41 "TFSSysAdmin",
42 "TFSUser"
43 };
44 }
45
46 /// <summary>
47 /// Singleton instance for Session manager
48 /// </summary>
49 public static SessionMgr Instance
50 {
51 get
52 {
53 if(instance == null)
54 {
55 instance = new SessionMgr();
56 }
57
58 return instance;
59 }
60 }
61
62
63 /// <summary>
64 /// Initial the session manager to add a parameter to the request url
65 /// </summary>
66 public void InitialSessionMgr()
67 {
68 try
69 {
70 context = HttpContext.Current;
71
72 string newPageUrl;
73 bool needRedirect = true;
74 string sKey;
75 int sValue = 0;
76 string pageUrl = context.Request.Url.ToString();
77
78 if (context.Request[pageIdentity] == null)
79 {
80 needRedirect = true;
81 sKey = pageUrl;
82
83 if (context.Session[pageIdentity] != null)
84 {
85 sValue = int.Parse(context.Session[pageIdentity].ToString());
86 }
87
88 sValue++;
89 }
90 else
91 {
92 needRedirect = false;
93 sKey = GetOrginalPageUrl(pageUrl);
94
95 // To process special scenarios:
96 // Scenario1: User reinput the url in IE's address box.
97 // Scenario2: User copied one url and paste it in a new tab
98 if(context.Request.UrlReferrer == null || context.Request.UrlReferrer.ToString().Trim().Length ==0)
99 {
100 RedirectToUrl(sKey);
101 }
102 else
103 {
104 string s = GetOrginalPageUrl(context.Request.UrlReferrer.ToString());
105 string s2 = GetOrginalPageUrl(pageUrl);
106 bool equal = s == s2;
107 //System.Diagnostics.Debug.Write("UrlReferrer["+ s +"] == Request[" +s2+ "]? " + equal.ToString()+"\n");
108 }
109 }
110
111 if(needRedirect)
112 {
113 context.Session[pageIdentity] = sValue;
114 newPageUrl = sKey + (sKey.IndexOf("?") > -1 ? "&" : "?") + pageIdentity + "=" + sValue;
115
116 //need to do: use script2 to redirect the page
117 RedirectToUrl(newPageUrl);
118 }
119 }
120 catch(Exception ex)
121 {
122 //Log Error
123 AddErrorLog("Session manager is initialized failed! - " + ex.ToString());
124 }
125 }
126
127
128
129 /// <summary>
130 /// Redirect to new url
131 /// </summary>
132 /// <param name="context">current http context</param>
133 /// <param name="url">new page url</param>
134 private void RedirectToUrl(string url)
135 {
136 /*
137 string script = "<script language=javascript>" +
138 "window.onload=function(){" +
139 "var tempa = document.createElement('a');" +
140 "tempa.href = '"+ sKey +"';" +
141 "document.getElementsByTagName('body')[0].appendChild(tempa);" +
142 "tempa.click();" +
143 "}" +
144 "</script>";
145 */
146
147 // We don't use javascript event "window.onload" to process the page redirection.
148 // Thus we won't affect the exists client script in the page
149 string script4 = @"<script language=javascript>
150 var counter=0,timer;
151 if(window != null)
152 {
153 timer = window.setInterval(forceToRedirect,100);
154 }
155
156 function forceToRedirect()
157 {
158 if(window.document != null && counter < 1)
159 {
160 counter++;
161 clearTimeout(timer);
162 redirectToUrl('" + url + @"');
163 }
164 }
165 </script>";
166
167 HttpContext.Current.Response.Write(script4);
168
169 }
170
171
172
173 /// <summary>
174 /// Get session object by the index "key"
175 /// </summary>
176 public object this[string key]
177 {
178 get
179 {
180 string k = GetSessionKey(key);
181 return HttpContext.Current.Session[k];
182 }
183 set
184 {
185 string k = GetSessionKey(key);
186 HttpContext.Current.Session[k] = value;
187 }
188 }
189
190 /// <summary>
191 /// Get session identity by orginal key
192 /// </summary>
193 /// <param name="key">orginal key</param>
194 /// <returns></returns>
195 private string GetSessionKey(string key)
196 {
197 for(int i = 0; i<excludeSessions.Length;i++)
198 {
199 if(excludeSessions[i] == key)
200 {
201 return key;
202 }
203 }
204
205 return this.GetSessionIdentity(key);
206 }
207
208 /// <summary>
209 /// Get the session identity by the key prefix
210 /// </summary>
211 /// <param name="orgKey">key prefix</param>
212 /// <returns>Session identity</returns>
213 public string GetSessionIdentity(string orgKey)
214 {
215 string newSessionkey;
216 if (HttpContext.Current.Request[pageIdentity] == null)
217 {
218 //Log Error
219 //AddErrorLog("Session manager for child window initialized failed! - " + HttpContext.Current.Request.Url.ToString());
220 return "EmptyKey";
221 }
222
223 newSessionkey = orgKey + "_" + HttpContext.Current.Request[pageIdentity].ToString();
224 return newSessionkey;
225 }
226
227 /// <summary>
228 /// Get the original url, remove the url parameter "pageIdentity"
229 /// </summary>
230 /// <param name="url">request.url</param>
231 /// <returns>url</returns>
232 private string GetOrginalPageUrl(string url)
233 {
234 string orgUrl = url.IndexOf("?") > -1 ? url.Substring(0,url.IndexOf("?")) : url;
235 return orgUrl;
236 }
237
238 /// <summary>
239 /// Add error log when error occurs in session manager
240 /// </summary>
241 /// <param name="errorMsg">Error message</param>
242 /// <returns>true or false</returns>
243 private bool AddErrorLog(string errorMsg)
244 {
245 TFSUser objUser=null;
246 if(context.Session!=null && context.Session.Keys.Count!=0 && context.Session["TFSUser"] != null)
247 {
248 objUser = (TFSUser) context.Session["TFSUser"];
249 }
250 string sStaffID = "SYSTEM";
251 if(objUser!=null)
252 {
253 sStaffID = objUser.StaffID;
254 }
255
256 return LogMgr.addErrorLog(errorMsg, sStaffID);
257 }
258
259 }
260 }
261
2 using System.Collections;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Net;
8
9 using TFSBussTier.objects;
10
11 namespace TFSBussTier.bussTier
12 {
13
14 /// <summary>
15 /// To Manage the session
16 /// </summary>
17 public class SessionMgr
18 {
19 // Session Identity for different page
20 public readonly string pageIdentity = "PageIdentity";
21
22 // Private instance for Session Manager
23 private static SessionMgr instance = null;
24
25 // Http context for current request
26 private HttpContext context;
27
28 //private static Hashtable excludeSessions;
29 private static string []excludeSessions;
30
31
32 public SessionMgr()
33 {
34 //excludeSessions.Add("CountryCode","CountryCode");
35 excludeSessions = new string[]{
36 "CountryCode",
37 "CTaxINI",
38 "iErrorLogID",
39 "LoginDefault",
40 "StaffID",
41 "TFSSysAdmin",
42 "TFSUser"
43 };
44 }
45
46 /// <summary>
47 /// Singleton instance for Session manager
48 /// </summary>
49 public static SessionMgr Instance
50 {
51 get
52 {
53 if(instance == null)
54 {
55 instance = new SessionMgr();
56 }
57
58 return instance;
59 }
60 }
61
62
63 /// <summary>
64 /// Initial the session manager to add a parameter to the request url
65 /// </summary>
66 public void InitialSessionMgr()
67 {
68 try
69 {
70 context = HttpContext.Current;
71
72 string newPageUrl;
73 bool needRedirect = true;
74 string sKey;
75 int sValue = 0;
76 string pageUrl = context.Request.Url.ToString();
77
78 if (context.Request[pageIdentity] == null)
79 {
80 needRedirect = true;
81 sKey = pageUrl;
82
83 if (context.Session[pageIdentity] != null)
84 {
85 sValue = int.Parse(context.Session[pageIdentity].ToString());
86 }
87
88 sValue++;
89 }
90 else
91 {
92 needRedirect = false;
93 sKey = GetOrginalPageUrl(pageUrl);
94
95 // To process special scenarios:
96 // Scenario1: User reinput the url in IE's address box.
97 // Scenario2: User copied one url and paste it in a new tab
98 if(context.Request.UrlReferrer == null || context.Request.UrlReferrer.ToString().Trim().Length ==0)
99 {
100 RedirectToUrl(sKey);
101 }
102 else
103 {
104 string s = GetOrginalPageUrl(context.Request.UrlReferrer.ToString());
105 string s2 = GetOrginalPageUrl(pageUrl);
106 bool equal = s == s2;
107 //System.Diagnostics.Debug.Write("UrlReferrer["+ s +"] == Request[" +s2+ "]? " + equal.ToString()+"\n");
108 }
109 }
110
111 if(needRedirect)
112 {
113 context.Session[pageIdentity] = sValue;
114 newPageUrl = sKey + (sKey.IndexOf("?") > -1 ? "&" : "?") + pageIdentity + "=" + sValue;
115
116 //need to do: use script2 to redirect the page
117 RedirectToUrl(newPageUrl);
118 }
119 }
120 catch(Exception ex)
121 {
122 //Log Error
123 AddErrorLog("Session manager is initialized failed! - " + ex.ToString());
124 }
125 }
126
127
128
129 /// <summary>
130 /// Redirect to new url
131 /// </summary>
132 /// <param name="context">current http context</param>
133 /// <param name="url">new page url</param>
134 private void RedirectToUrl(string url)
135 {
136 /*
137 string script = "<script language=javascript>" +
138 "window.onload=function(){" +
139 "var tempa = document.createElement('a');" +
140 "tempa.href = '"+ sKey +"';" +
141 "document.getElementsByTagName('body')[0].appendChild(tempa);" +
142 "tempa.click();" +
143 "}" +
144 "</script>";
145 */
146
147 // We don't use javascript event "window.onload" to process the page redirection.
148 // Thus we won't affect the exists client script in the page
149 string script4 = @"<script language=javascript>
150 var counter=0,timer;
151 if(window != null)
152 {
153 timer = window.setInterval(forceToRedirect,100);
154 }
155
156 function forceToRedirect()
157 {
158 if(window.document != null && counter < 1)
159 {
160 counter++;
161 clearTimeout(timer);
162 redirectToUrl('" + url + @"');
163 }
164 }
165 </script>";
166
167 HttpContext.Current.Response.Write(script4);
168
169 }
170
171
172
173 /// <summary>
174 /// Get session object by the index "key"
175 /// </summary>
176 public object this[string key]
177 {
178 get
179 {
180 string k = GetSessionKey(key);
181 return HttpContext.Current.Session[k];
182 }
183 set
184 {
185 string k = GetSessionKey(key);
186 HttpContext.Current.Session[k] = value;
187 }
188 }
189
190 /// <summary>
191 /// Get session identity by orginal key
192 /// </summary>
193 /// <param name="key">orginal key</param>
194 /// <returns></returns>
195 private string GetSessionKey(string key)
196 {
197 for(int i = 0; i<excludeSessions.Length;i++)
198 {
199 if(excludeSessions[i] == key)
200 {
201 return key;
202 }
203 }
204
205 return this.GetSessionIdentity(key);
206 }
207
208 /// <summary>
209 /// Get the session identity by the key prefix
210 /// </summary>
211 /// <param name="orgKey">key prefix</param>
212 /// <returns>Session identity</returns>
213 public string GetSessionIdentity(string orgKey)
214 {
215 string newSessionkey;
216 if (HttpContext.Current.Request[pageIdentity] == null)
217 {
218 //Log Error
219 //AddErrorLog("Session manager for child window initialized failed! - " + HttpContext.Current.Request.Url.ToString());
220 return "EmptyKey";
221 }
222
223 newSessionkey = orgKey + "_" + HttpContext.Current.Request[pageIdentity].ToString();
224 return newSessionkey;
225 }
226
227 /// <summary>
228 /// Get the original url, remove the url parameter "pageIdentity"
229 /// </summary>
230 /// <param name="url">request.url</param>
231 /// <returns>url</returns>
232 private string GetOrginalPageUrl(string url)
233 {
234 string orgUrl = url.IndexOf("?") > -1 ? url.Substring(0,url.IndexOf("?")) : url;
235 return orgUrl;
236 }
237
238 /// <summary>
239 /// Add error log when error occurs in session manager
240 /// </summary>
241 /// <param name="errorMsg">Error message</param>
242 /// <returns>true or false</returns>
243 private bool AddErrorLog(string errorMsg)
244 {
245 TFSUser objUser=null;
246 if(context.Session!=null && context.Session.Keys.Count!=0 && context.Session["TFSUser"] != null)
247 {
248 objUser = (TFSUser) context.Session["TFSUser"];
249 }
250 string sStaffID = "SYSTEM";
251 if(objUser!=null)
252 {
253 sStaffID = objUser.StaffID;
254 }
255
256 return LogMgr.addErrorLog(errorMsg, sStaffID);
257 }
258
259 }
260 }
261