Global.asax是ASP.Net应用程序的一个文件,用来处理Application级别的事情。可以添加自定义代码到这个文件,详细使用方式见 http://msdn.microsoft.com/en-us/library/2027ewzw.aspx。
既然SharePoint也是ASP.Net应用程序,那么也可以使用这中处理方式。唯一的区别是必须继承Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication这个类,在SharePoint2010中,网站根目录下已包含这个文件,用记事本打开,可以看到这样的两断代码:
<%@ Assembly Name="Microsoft.SharePoint"%> <%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
现在修改这些代码为:
<%@ Assembly Name="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24a3a6cf980145af"%> <%@ Application Language="C#" Inherits="MyAssembly.Global" %>
这些做完后,需要创建个程序集MyAssembly和这个程序集中的类文件Global,Global类必须继承Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication这个类,现在可以在这个类来编写代码了,下面是一个简单示例:
namespace MyAssembly { public class Global : Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication { protected void Application_AuthenticateRequest(object sender, EventArgs e) { if(HttpContext.Current.Request.Url.AbsolutePath.EndsWith("/securepage.aspx")) { throw new UnauthorizedAccessException(); } } } }
这个示例,AuthenticateRequest event hanlder用来自定义认证逻辑,阻止部分人员访问SharePoint资源。当然你也可以添加Global.asax一些其他操作。