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 ///
15 /// To Manage the session
16 ///
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 ///
47 /// Singleton instance for Session manager
48 ///
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 ///
64 /// Initial the session manager to add a parameter to the request url
65 ///
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 ///
130 /// Redirect to new url
131 ///
132 /// current http context
133 /// new page url
134 private void RedirectToUrl( string url)
135 {
136 /*
137 string 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 = @" " ;
166
167 HttpContext.Current.Response.Write(script4);
168
169 }
170
171
172
173 ///
174 /// Get session object by the index "key"
175 ///
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 ///
191 /// Get session identity by orginal key
192 ///
193 /// orginal key
194 ///
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 ///
209 /// Get the session identity by the key prefix
210 ///
211 /// key prefix
212 ///
Session identity
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 ///
228 /// Get the original url, remove the url parameter "pageIdentity"
229 ///
230 /// request.url
231 ///
url
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 ///
239 /// Add error log when error occurs in session manager
240 ///
241 /// Error message
242 ///
true or false
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 ///
15 /// To Manage the session
16 ///
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 ///
47 /// Singleton instance for Session manager
48 ///
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 ///
64 /// Initial the session manager to add a parameter to the request url
65 ///
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 ///
130 /// Redirect to new url
131 ///
132 /// current http context
133 /// new page url
134 private void RedirectToUrl( string url)
135 {
136 /*
137 string 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 = @" " ;
166
167 HttpContext.Current.Response.Write(script4);
168
169 }
170
171
172
173 ///
174 /// Get session object by the index "key"
175 ///
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 ///
191 /// Get session identity by orginal key
192 ///
193 /// orginal key
194 ///
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 ///
209 /// Get the session identity by the key prefix
210 ///
211 /// key prefix
212 ///
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 ///
228 /// Get the original url, remove the url parameter "pageIdentity"
229 ///
230 /// request.url
231 ///
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 ///
239 /// Add error log when error occurs in session manager
240 ///
241 /// Error message
242 ///
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