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>

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

}

}

}

}

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

我们可以看到,这种用户验证机制,在我们的程序越宠大,它所带来的好处就越多,而且他还有很多值得我们发掘的地方!

希望大家能和我互相交流!谢谢!

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2021-12-18
  • 2021-09-04
  • 2022-01-07
相关资源
相似解决方案