⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 global.asax

📁 asp.net技术内幕的书配源码
💻 ASAX
字号:
<%@ 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -