基于角色的身份验证
web.config
<authentication mode="Forms">
<forms name="app" loginUrl="bb.aspx"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
Roles.xml
<?xml version="1.0" encoding="utf-8" ?>
<roles>
<user
name="Bob"
roles="Sales" />
<user
name="Jane"
roles="Supervisor,Sales" />
</roles>
bb.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.Security .FormsAuthentication .RedirectFromLoginPage(this.TextBox1 .Text,false);
![](/Images/OutliningIndicators/InBlock.gif)
}
Global.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
![](/Images/OutliningIndicators/InBlock.gif)
string strUserName;
XmlDocument objRoles;
XmlNode objNode;
string strXPath;
![](/Images/OutliningIndicators/InBlock.gif)
objRoles = GetRoles();
if ( Context.Request.IsAuthenticated )
{
strUserName = Context.User.Identity.Name;
strXPath = string.Format( "user[@name='{0}']", strUserName );
objNode = objRoles.DocumentElement.SelectSingleNode( strXPath );
if (objNode != null)
{
string[] arrRoles = objNode.Attributes["roles"].Value.Split (new char[] {','});
// 这很重要返回为 string[] 类型,要保证被分割.......
![](/Images/OutliningIndicators/InBlock.gif)
foreach(string s in arrRoles)
{
this.Response .Write (s+ arrRoles.Length .ToString ());
}
Context.User = new GenericPrincipal( Context.User.Identity, arrRoles);
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
XmlDocument GetRoles()
{
XmlDocument objRoles;
![](/Images/OutliningIndicators/InBlock.gif)
objRoles = (XmlDocument)Context.Cache[ "Roles" ];
if ( objRoles == null )
{
objRoles = new XmlDocument();
objRoles.Load( Server.MapPath( "Roles.xml" ) );
Context.Cache.Insert( "Roles", objRoles, new CacheDependency( Server.MapPath( "Roles.xml" ) ) );
}
return objRoles;
}
Default.aspx
if ( User.IsInRole( "Sales" ) )
{
Response.Write( "You have Sales permissions!" );
// User.Identity .AuthenticationType.ToString ();
![](/Images/OutliningIndicators/InBlock.gif)
}
if (User.IsInRole ("Supervisor"))
{
Response.Write( "You have supervisor permissions!" );
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
Roles.xml
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
bb.aspx
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Global.asax
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
// 这很重要返回为 string[] 类型,要保证被分割.......
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Default.aspx
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)