现在的位置: 首页 > 综合 > 正文

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

2011年10月30日 ⁄ 综合 ⁄ 共 3984字 ⁄ 字号 评论关闭

HttpContext类包含了个别HTTP请求的所有特定HTTP信息。这个示例主要是讲如何使用HttpContext类中的User属性来实现用户验证!

用户验证是大部分ASP.NET WEB应用程序都要用到的,它在整个应用程序中占有很重要的地位,在.NET中,包含了很多种用户验证方式,如众所周知的PassPort认证,Windows认证,Form认证等等,可是这些都很难满足我们在实际应用中的需求,以致于很多朋友都是自己另外写代码来实现自己需要的功能,这让我们在安全性以及系统效率上要考虑很多。

实际上,ASP.NET中内置的用户验证机制功能非常强大,同时也具有非常好的的可扩展性,它能够在HttpContext对象中生成一个名为User的属性,这个属性能让我们访问各种信息,包括用户是否已验证,用户的类型,用户名等等,我们还可以对该属性的功能进性扩展,以实现我们的要求。

分配给HttpContext.User的对象必须实现IPrincipal接口,而Iprincipal定义的属性之一是Identity,它必须实现Iidentity接口。因为,我们只要写了实现这两个接口的类,就可以在这些类中添加任何我们所需要的功能。

首先,我们创建两个实现IprincipalIidentity的类,分别为MyIprincipalMyIdentity

 

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;
            }

        }

        
IPrincipal 成员
    }

}

 

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;
            }

        }


        
IIdentity 成员
    }

}

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

 

MyPage.cs

using System;
using System.Collections;

namespace HttpContextUserEG
{
    
/// <summary>
    
/// MyPage 的摘要说明。
    
/// </summary>
    
/// 继承自Page类

    public class MyPage : System.Web.UI.Page
    
{
        
public

抱歉!评论已关闭.