当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > HttpContext类包含了个别HTTP请求的所有特定HTTP信息。

ASP.NET
ReferenceEquals, == , Equals 比较
VB.NET窗口渐淡关闭
使用简单的DepthBuffer 和使用Lights
VB下使用adodb.command 执行存储过程注意
Sendkeys 和 Sendmessage 使用技巧一例
使用IndexBuffer(索引)
探讨C#中字符串的加密
常用加密算法
如何更新父窗体
当SESSION失效时自动转到其它页面
Sendkeys 和 Sendmessage 使用技巧一例 选择自 northwolves 的 Blog
1.DotNet(.Net):新平台,C#:新语言
Web下打印的实现
在.NET中实现彩色光标,动画光标和自定义光标[引自孟子前辈作品]
alert在asp.net中如何使用??
替换HTML代码
h2reg的一些使用经验
C#2.0 新特性探究(一) 模拟List和内置算法
在网页中添加Flash的播放或者背景音乐
Cookie 的写入与读取

ASP.NET 中的 HttpContext类包含了个别HTTP请求的所有特定HTTP信息。


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 86 ::
收藏到网摘: n/a

代码HttpContext类包含了个别HTTP请求的所有特定HTTP信息。这个示例主要是讲如何使用HttpContext类中的User属性来实现用户验证!用户验证是大部分ASP.NET WEB应用程序都要用到的,它在整个应用程序中占有很重要的地位,在.NET中,包含了很多种用户验证方式,如众所周知的PassPort认证,Windows认证,Form认证等等,可是这些都很难满足我们在实际应用中的需求,以致于很多朋友都是自己另外写代码来实现自己需要的功能,这让我们在安全性以及系统效率上要考虑很多。实际上,ASP.NET中内置的用户验证机制功能非常强大,同时也具有非常好的的可扩展性,它能够在HttpContext对象中生成一个名为User的属性,这个属性能让我们访问各种信息,包括用户是否已验证,用户的类型,用户名等等,我们还可以对该属性的功能进性扩展,以实现我们的要求。分配给HttpContext.User的对象必须实现IPrincipal接口,而Iprincipal定义的属性之一是Identity,它必须实现Iidentity接口。因为,我们只要写了实现这两个接口的类,就可以在这些类中添加任何我们所需要的功能。首先,我们创建两个实现Iprincipal和Iidentity的类,分别为MyIprincipal和MyIdentityMyIprincipal.csusing System;using System.Collections;namespace HttpContextUserEG{ /// /// MyPrincipal 的摘要说明。 /// /// 实现IPrincipal接口 public class MyPrincipal : System.Security.Principal.IPrincipal { private System.Security.Principal.IIdentity identity; private ArrayList roleList; public MyPrincipal(string userID,string password) { // // TODO: 在此处添加构造函数逻辑 // identity = new MyIdentity(userID,password); if(identity.IsAuthenticated) { //如果通过验证则获取该用户的Role,这里可以修改为从数据库中 //读取指定用户的Role并将其添加到RoleList中,本例中直接为用户添加一个Admin角色 roleList = new ArrayList(); roleList.Add("Admin"); } else { // do nothing } } public ArrayList RoleList { get { return roleList; } } #region IPrincipal 成员 public System.Security.Principal.IIdentity Identity { get { // TODO: 添加 MyPrincipal.Identity getter 实现 return identity; } set { identity = value; } } public bool IsInRole(string role) { // TODO: 添加 MyPrincipal.IsInRole 实现 return roleList.Contains(role);; } #endregion }}MyIdentity.csusing System;namespace HttpContextUserEG{ /// /// MyIdentity 的摘要说明。 /// /// 实现IIdentity接口 public class MyIdentity : System.Security.Principal.IIdentity { private string userID; private string password; public MyIdentity(string currentUserID,string currentPassword) { // // TODO: 在此处添加构造函数逻辑 // userID = currentUserID; password = currentPassword; } private bool CanPass() { //这里朋友们可以根据自己的需要改为从数据库中验证用户名和密码, //这里为了方便我直接指定的字符串 if(userID == "yan0lovesha" && password == "iloveshasha") { return true; } else { return false; } } public string Password { get { return password; } set { password = value; } } #region IIdentity 成员 public bool IsAuthenticated { get { // TODO: 添加 MyIdentity.IsAuthenticated getter 实现 return CanPass(); } } public string Name { get { // TODO: 添加 MyIdentity.Name getter 实现 return userID; } } //这个属性我们可以根据自己的需要来灵活使用,在本例中没有用到它 public string AuthenticationType { get { // TODO: 添加 MyIdentity.AuthenticationType getter 实现 return null; } } #endregion }}在完成了这两个类之后我们还要创建一个自己的Page类,来配合我们的验证,这样做还可以让我们不必在每个页面中都写相同的Page_Load事件。这里我们将其命名为MyPage,继承自Page类MyPage.csusing System;using System.Collections;namespace HttpContextUserEG{ /// /// MyPage 的摘要说明。 /// /// 继承自Page类 public class MyPage : System.Web.UI.Page { public MyPage() { // // TODO: 在此处添加构造函数逻辑 // } protected override void OnInit(EventArgs e) { base.OnInit (e); this.Load +=new EventHandler(MyPage_Load); } //在页面加载的时候从缓存中提取用户信息 private void MyPage_Load(object sender, System.EventArgs e) { if(Context.User.Identity.IsAuthenticated) { if(Context.Cache["UserMessage"] != null) { Hashtable userMessage = (Hashtable)Context.Cache["UserMessage"]; MyPrincipal principal = new MyPrincipal(userMessage["UserID"].ToString(),userMessage["UserPassword"].ToString()); Context.User = principal; } } } }}下面就是我们的界面WebForm.aspx和WebForm.aspx.csW