本例实现一个简单的基于角色的安全控制的架构的实现。本程序的用户角色存储在代码文件中是为了容易理解,在实际的开发中应该将角色存放到数据库中。
注意:有省略号的部分和原来用vs.net中代码相同,不需要改变,这里只是写出了需要改变或者要添加的部分。
根目录:
login.aspx.cs文件代码:
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
……………..
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if(FormsAuthentication.Authenticate(TextBox1.Text,TextBox2.Text))
{
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text,false);
}
}
}
}
Global.asax.cs代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Security.Principal;
namespace WebApplication3
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
……………………
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if(Request.IsAuthenticated)
{
if(User.Identity.Name.ToLower().CompareTo("test1")==0)
{
string[] roles2={"role2"};
Context.User=new GenericPrincipal(User.Identity,roles2);
}
if(User.Identity.Name.ToLower().CompareTo("test")==0)
{
string[] roles1={"Administrator"};
Context.User=new GenericPrincipal(User.Identity,roles1);
}
}
}
…………………………….
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
default.aspx.cs代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication3
{
/// <summary>
/// defualt 的摘要说明。
/// </summary>
public class defualt : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (User.IsInRole("role2"))
{
Response.Redirect("xyz/nothing.aspx");
}
if (User.IsInRole("Administrator"))
{
Response.Redirect("abc/success.aspx");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Web.config代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
……....
<authentication mode="Forms" >
<forms loginUrl="Login.aspx" protection="All" timeout="30" path="/">
<credentials passwordFormat="Clear">
<user password="test"></user>
<user password="test1"></user>
</credentials>
</forms>
</authentication>
<!-- 授权
此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名
(未经身份验证的)用户。
-->
<authorization>
<deny users="?" /> <!-- 允许所有用户 -->
<!-- <allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
</authorization>
………
</system.web>
</configuration>
目录:abc
success.aspx文件代码即为正常页面(代码略)
Web.config文件代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authorization>
<allow roles="Administrator" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
目录:xyz
Nothing.aspx文件代码为正常页面(代码略)
Web.config文件代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authorization>
<allow roles="role2"/>
<deny users="*" />
</authorization>
</system.web>
</configuration>