asp.net c# Design Pattern WebService Windows Report Control Mobile Symbin Windows Phone 7 Visual Studio 2011

使用HttpContext的User属性来实现用户验证

Posted on 2007-12-07 15:41  笑缘  阅读(428)  评论(0编辑  收藏  举报

    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和MyIdentity 

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; 

}
 

}
 

IPrincipal 成员 

}
 

}
 


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; 

}
 

}
 

 

IIdentity 成员 

}
 

}
 


    在完成了这两个类之后我们还要创建一个自己的Page类,来配合我们的验证,这里我们将其命名为MyPage,继承自Page类
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

<%@ 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 id="Form1" method="post" runat="server"> 

<P><FONT face="宋体">用户名: 

<asp:TextBox id="tbxUserID" runat="server"></asp:TextBox><BR> 

密 码: 

<asp:TextBox id="tbxPassword" runat="server" TextMode="Password"></asp:TextBox></FONT></P> 

<P><FONT face="宋体"> 

<asp:Button id="btnLogin" runat="server" Text="登录"></asp:Button> 

<asp:Label id="lblLoginMessage" runat="server"></asp:Label></FONT></P> 

<P><FONT face="宋体"> 

<asp:Panel id="Panel1" runat="server" Visible="False"> 

<P> 

<asp:Button id="btnAdmin" runat="server" Text="角色1"></asp:Button> 

<asp:Button id="btnUser" runat="server" Text="角色2"></asp:Button></P> 

<P> 

<asp:Label id="lblRoleMessage" runat="server"></asp:Label></P> 

</asp:Panel> 

<P></P> 

</FONT> 

</form> 

</body> 

</HTML> 


using System; 

using System.Collections; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Web; 

using System.Web.Caching; 

using System.Web.SessionState; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Web.UI.HtmlControls; 

 

namespace HttpContextUserEG 



/// <summary> 

/// WebForm1 的摘要说明。 

/// </summary> 

/// 将这里本来继承自Page类改为继承自我们自己的MyPage类 


public class WebForm1 : HttpContextUserEG.MyPage 



protected System.Web.UI.WebControls.TextBox tbxUserID; 

protected System.Web.UI.WebControls.TextBox tbxPassword; 

protected System.Web.UI.WebControls.Panel Panel1; 

protected System.Web.UI.WebControls.Button btnAdmin; 

protected System.Web.UI.WebControls.Button btnUser; 

protected System.Web.UI.WebControls.Label lblRoleMessage; 

protected System.Web.UI.WebControls.Label lblLoginMessage; 

protected System.Web.UI.WebControls.Button btnLogin; 

 

private void Page_Load(object sender, System.EventArgs e) 



// 在此处放置用户代码以初始化页面 

}
 

 

Web 窗体设计器生成的代码 

 

private void btnLogin_Click(object sender, System.EventArgs e) 



MyPrincipal principal 
= new MyPrincipal(tbxUserID.Text,tbxPassword.Text); 

if(!principal.Identity.IsAuthenticated) 



lblLoginMessage.Text 
= "用户名或密码不正确"

Panel1.Visible 
= false

}
 

else 



// 如果用户通过验证,则将用户信息保存在缓存中,以备后用 

// 在实际中,朋友们可以尝试使用用户验证票的方式来保存用户信息,这也是.NET内置的用户处理机制 

Context.User 
= principal; 

Hashtable userMessage 
= new Hashtable(); 

userMessage.Add(
"UserID",tbxUserID.Text); 

userMessage.Add(
"UserPassword",tbxPassword.Text); 

Context.Cache.Insert(
"UserMessage",userMessage); 

lblLoginMessage.Text 
= tbxUserID.Text + "已经登录"

Panel1.Visible 
= true

}
 

}
 

 

private void btnAdmin_Click(object sender, System.EventArgs e) 



// 验证用户的Role中是否包含Admin 

if(Context.User.IsInRole("Admin")) 



lblRoleMessage.Text 
= "用户" + ((MyPrincipal)Context.User).Identity.Name + "属于Admin组"

}
 

else 



lblRoleMessage.Text 
= "用户" + Context.User.Identity.Name + "不属于Admin组"

}
 

}
 

 

private void btnUser_Click(object sender, System.EventArgs e) 



// 验证用户的Role中是否包含User 

if(Context.User.IsInRole("User")) 



lblRoleMessage.Text 
= "用户" + Context.User.Identity.Name + "属于User组"

}
 

else 



lblRoleMessage.Text 
= "用户" + Context.User.Identity.Name + "不属于User组"

}
 

}
 

}
 

}
 


    代码部分介绍完了,朋友们可以自己试试来看到效果,在这个例子中很多地方都为了方便而直接给予赋值,在实际应用中,这些将是从数据库或从其它配置文件中得到,而这种方法的可扩展性是非常高的,我们可以根据自己的需要来扩展MyIprincipal和MyIdentity类的功能。比如我们可以添加一个IsInPermission来使用户不仅属于角色,每个角色还可以拥有不同的权限。
    在本例中,在用户验证过后是通过使用缓存来保存已验证用户的信息的,我们还可以尝试使用用户验证票的方式来实现。

Copyright © 2024 笑缘
Powered by .NET 8.0 on Kubernetes

asp.net c# Design Pattern WebService Windows Report Control Mobile Windows Phone Visual Studio
asp.net c# Design Pattern WebService Windows Report Control Mobile