在我的上一篇《使用 HttpContext 中的 User 属性来实现用户身份验证》 中已经讲了怎样来使用 HttpContext.User 属性来实现用户身份验证,并且还写了一个示例程序。但是,在上一篇文章中,我们使用的是系统缓存来保存用户的登录信息,这无疑是占用系统资源的一种做法,那有没有更好的办法呢?我在上一章中说过大家可以尝试使用用户验证票的方式来保存用户登录信息的,这种方式是基于 Cookie 原理来实现的,因此避免了使用缓存所带来的困扰。我们已经有了上一篇的基础,这次只需要在它的基础上稍加修改就可以了。
要使用这种方法,我们首先要配置 web.config 文件,把
1<authentication> 节点的属性 mode 改为 Forms ,再在该节点中添加 <forms loginurl="~\WebForm1.aspx" name="ContextUser" protection="None" slidingexpiration="true"></forms> ,这些代表的意思可以在 MSDN 上查到,这里就不多作解释了。
2
3下面是修改后的代码:
4
5** MyPage.cs **
6
7** **
8
9using System;
10
11using System.Collections;
12
13namespace HttpContextUserEG
14
15{
16
17/// <summary>
18
19/// MyPage 的摘要说明。
20
21/// </summary>
22
23/// 继承自 Page 类
24
25public class MyPage : System.Web.UI.Page
26
27{
28
29public MyPage()
30
31{
32
33//
34
35// TODO: 在此处添加构造函数逻辑
36
37//
38
39}
40
41protected override void OnInit(EventArgs e)
42
43{
44
45base .OnInit (e);
46
47this .Load += new EventHandler(MyPage_Load);
48
49}
50
51// 在页面加载的时候从缓存中提取用户信息
52
53private void MyPage_Load( object sender, System.EventArgs e)
54
55{
56
57if (Context.User.Identity.IsAuthenticated)
58
59{
60
61if (!(Context.User is MyPrincipal))
62
63{
64
65MyPrincipal principal = new MyPrincipal(Context.User.Identity.Name);
66
67Context.User = principal;
68
69}
70
71}
72
73}
74
75}
76
77}
78
79** **
80
81** MyPrincipal.cs **
82
83using System;
84
85using System.Collections;
86
87namespace HttpContextUserEG
88
89{
90
91/// <summary>
92
93/// MyPrincipal 的摘要说明。
94
95/// </summary>
96
97/// 实现 IPrincipal 接口
98
99public class MyPrincipal : System.Security.Principal.IPrincipal
100
101{
102
103private System.Security.Principal.IIdentity identity;
104
105private ArrayList roleList;
106
107public MyPrincipal( string userID)
108
109{
110
111//
112
113// TODO: 在此处添加构造函数逻辑
114
115//
116
117identity = new MyIdentity(userID);
118
119roleList = new ArrayList();
120
121roleList.Add("Admin");
122
123}
124
125public static MyPrincipal ValidateLogin( string userID, string password)
126
127{
128
129if (userID == "yan0lovesha" && password == "iloveshasha")
130
131{
132
133return new MyPrincipal(userID);
134
135}
136
137else
138
139return null ;
140
141}
142
143public ArrayList RoleList
144
145{
146
147get
148
149{
150
151return roleList;
152
153}
154
155}
156
157#region IPrincipal 成员
158
159public System.Security.Principal.IIdentity Identity
160
161{
162
163get
164
165{
166
167// TODO: 添加 MyPrincipal.Identity getter 实现
168
169return identity;
170
171}
172
173set
174
175{
176
177identity = value ;
178
179}
180
181}
182
183public bool IsInRole( string role)
184
185{
186
187// TODO: 添加 MyPrincipal.IsInRole 实现
188
189return roleList.Contains(role);;
190
191}
192
193#endregion
194
195}
196
197}
198
199** MyIdentity.cs **
200
201** **
202
203using System;
204
205namespace HttpContextUserEG
206
207{
208
209/// <summary>
210
211/// MyIdentity 的摘要说明。
212
213/// </summary>
214
215/// 实现 IIdentity 接口
216
217public class MyIdentity : System.Security.Principal.IIdentity
218
219{
220
221private string userID;
222
223private string password;
224
225public MyIdentity( string currentUserID)
226
227{
228
229//
230
231// TODO: 在此处添加构造函数逻辑
232
233//
234
235userID = currentUserID;
236
237password = "iloveshasha"; // 这里实际上是从数据库中获得的密码
238
239}
240
241private bool CanPass()
242
243{
244
245// 这里朋友们可以根据自己的需要改为从数据库中验证用户名和密码,
246
247// 这里为了方便我直接指定的字符串
248
249if (userID == "yan0lovesha" && password == "iloveshasha")
250
251{
252
253return true ;
254
255}
256
257else
258
259{
260
261return false ;
262
263}
264
265}
266
267public string Password
268
269{
270
271get
272
273{
274
275return password;
276
277}
278
279set
280
281<P class=MsoNormal style="MARGIN: 0cm 0cm 0p</authentication>