ttpContext类包含了个别HTTP请求的所有特定HTTP信息。这个示例主要是讲如何使用HttpContext类中的User属性来实现用户验证! 
用户验证是大部分ASP.NET WEB应用程序都要用到的,它在整个应用程序中占有很重要的地位,在.NET中,包含了很多种用户验证方式,如众所周知的PassPort认证,Windows认证,Form认证等等,可是这些都很难满足我们在实际应用中的需求,以致于很多朋友都是自己另外写代码来实现自己需要的功能,这让我们在安全性以及系统效率上要考虑很多。 
实际上,ASP.NET中内置的用户验证机制功能非常强大,同时也具有非常好的的可扩展性,它能够在HttpContext对象中生成一个名为User的属性,这个属性能让我们访问各种信息,包括用户是否已验证,用户的类型,用户名等等,我们还可以对该属性的功能进性扩展,以实现我们的要求。 
分配给HttpContext.User的对象必须实现IPrincipal接口,而Iprincipal定义的属性之一是Identity,它必须实现Iidentity接口。因为,我们只要写了实现这两个接口的类,就可以在这些类中添加任何我们所需要的功能。 
首先,我们创建两个实现Iprincipal和Iidentity的类,分别为MyIprincipal和MyIdentity 

MyIprincipal.cs  

using System; 
using System.Collections; 

namespace HttpContextUserEG 

    /// <summary> 
    /// MyPrincipal 的摘要说明。 
    /// </summary> 
    /// 实现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并将其添加到Role中,本例中直接为用户添加一个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.cs  

using System; 

namespace HttpContextUserEG 

    /// <summary> 
    /// MyIdentity 的摘要说明。 
    /// </summary> 
    /// 实现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类,来配合我们的验证,这里我们将其命名为MyPage,继承自Page类 

MyPage.cs  

using System; 
using System.Collections; 

namespace HttpContextUserEG 

    /// <summary> 
    /// MyPage 的摘要说明。 
    /// </summary> 
    /// 继承自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.cs 

WebForm.aspx  

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="HttpContextUserEG.WebForm1" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<HTML> 
    <HEAD> 
       <title>WebForm1</title> 
       <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> 
       <meta content="C#" name="CODE_LANGUAGE"> 
       <meta content="JavaScript" name="vs_defaultClientScript"> 
       <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> 
    </HEAD> 
    <body> 
       <form ; 
          } 
       } 
    } 

相关文章:

  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
  • 2022-03-04
  • 2021-08-25
  • 2021-08-10
猜你喜欢
  • 2021-12-26
  • 2021-12-18
  • 2021-08-14
  • 2021-08-27
  • 2021-09-23
相关资源
相似解决方案