global.asax

来自「asp.net技术内幕的书配源码」· ASAX 代码 · 共 41 行

ASAX
41
字号
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Xml" %>

<script language="C#" runat=server>

void Application_AuthenticateRequest( object s, EventArgs e ) {
	string  strUserName;
	XmlDocument objRoles;
	XmlNode objNode;
	string  strXPath;

	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();
			Context.User = new GenericPrincipal( Context.User.Identity, arrRoles );
		}
	}
}


XmlDocument GetRoles() {
	XmlDocument objRoles;

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

</Script>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?