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

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

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

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

<titleWebForm1</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

<

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航