用户验证是大部分ASP.NET WEB应用程序都要用到的,它在整个应用程序中占有很重要的地位,在.NET中,包含了很多种用户验证方式,如众所周知的PassPort认证,Windows认证,Form认证等等,可是这些都很难满足我们在实际应用中的需求,以致于很多朋友都是自己另外写代码来实现自己需要的功能,这让我们在安全性以及系统效率上要考虑很多。
实际上,ASP.NET中内置的用户验证机制功能非常强大,同时也具有非常好的的可扩展性,它能够在HttpContext对象中生成一个名为 User的属性,这个属性能让我们访问各种信息,包括用户是否已验证,用户的类型,用户名等等,我们还可以对该属性的功能进性扩展,以实现我们的要求。
分配给HttpContext.User的对象必须实现IPrincipal接口,而Iprincipal定义的属性之一是Identity,它必须实现Iidentity接口。因为,我们只要写了实现这两个接口的类,就可以在这些类中添加任何我们所需要的功能。
首先,我们创建一个项目,然后创建两个实现Iprincipal和Iidentity的类(分别放在相应的cs文件中),分另为MyIprincipal和MyIdentity
Code for MyIprincipal.cs
using System.Collections;//
///
/// Summary description for MyIprincipal
///
public class MyIprincipal : System.Security.Principal.IPrincipal
{
private System.Security.Principal.IIdentity identity;
private ArrayList roleList;
public MyIprincipal(string userID, string passwrod)
{
identity = new MyIdentity(userID, passwrod);
if (identity.IsAuthenticated)
{
roleList = new ArrayList();
roleList.Add("Admin");
}
}
public ArrayList RoleList
{
get
{
return roleList;
}
}
public System.Security.Principal.IIdentity Identity
{
get
{
return identity;
}
set
{
identity = value;
}
}
public bool IsInRole(string role)
{
return roleList.Contains(role);
}
public MyIprincipal()
{
//
// TODO: Add constructor logic here
//
}
}
*******************************
Code for MyIdentity.cs
///
/// Summary description for MyIdentity
///
public class MyIdentity : System.Security.Principal.IIdentity
{
private string userID;
private string password;
public MyIdentity( string currentUserID,string currentPassword)
{
userID = currentUserID;
password = currentPassword;
}
private bool CanPass()
{
if (userID == "kevin" && password == "kevin")
{
return true;
}
else
{
return false;
}
}
public string Name
{
get
{
return userID;
}
}
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
public bool IsAuthenticated
{
get
{
return CanPass();
}
}
public string AuthenticationType
{
get
{
return null;
}
}
public MyIdentity()
{
//
// TODO: Add constructor logic here
//
}
}
********************
Code for Default.aspx
<body>
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtName"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<asp:TextBox ID="txtPwd"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" Text=""
runat="server"></asp:Label>
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
*************************
Code for Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MyIprincipal pri = new MyIprincipal(txtName.Text.ToString(), txtPwd.Text.ToString());
if (pri.Identity.IsAuthenticated)
{
this.lblMsg.Text = "OK";
}
else
{
lblMsg.Text = "No";
}
}
}


No comments:
Post a Comment