Global.asax中配置:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
List<string> programList = new List<string>();
programList.Add("WEBR1010");
Application["ProgramList"] = programList;
}
1.利用IHttpModule
代码
using System;
using System.Web;
using System.Collections.Generic;
namespace WintonAuthorization
{
/// <summary>
/// HttpModule授权类
/// </summary>
public class AuthModule :IHttpModule
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="application"></param>
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
//application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
/// <summary>
/// 文件请求开始判断
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpRequest reqest = application.Context.Request;
//url请求
string fileName = string.Empty;
string urlStr = reqest.Url.AbsolutePath;
if (!string.IsNullOrEmpty(urlStr))
{
string[] urlArray = urlStr.Split('/');
fileName = urlArray[urlArray.Length - 1].ToUpper().Replace(".ASPX","");
}
//大等于8位的才符合條件
if (!string.IsNullOrEmpty(fileName) && fileName.Length >= 8)
{
//取前8位
fileName = fileName.Substring(0, 8);
//是否是符合要求的程式
bool isOKProgram = false;
//符合:前3位等于'WEB',第4位為字母,后4位為數字的才需要判斷
if ("WEB" == fileName.Substring(0, 3))
{
if (char.IsLetter(fileName[3]))
{
if (char.IsDigit(fileName[4]) && char.IsDigit(fileName[5]) && char.IsDigit(fileName[6]) && char.IsDigit(fileName[7]))
{
isOKProgram = true;
}
}
}
if (isOKProgram)
{
//是否存在文件
List<string> programList = application.Context.Application["ProgramList"] as List<string>;
if (null != programList && 0 < programList.Count && programList.Contains(fileName))
{
}
else
{
HttpResponse Response = application.Context.Response;
Response.Write("<script >alert('該程式沒有授權!');history.back();</script>");
}
}
}
}
/// <summary>
/// 請求結束
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void Application_EndRequest(Object source, EventArgs e)
{
}
/// <summary>
/// 釋放
/// </summary>
public void Dispose()
{
}
}
}
Web.config中 配置:
<httpModules>
<add name="Auth" type="WintonAuthorization.AuthModule,WintonAuthorization" />
</httpModules>
2.利用IHttpHandler
代码
using System;
using System.Collections.Generic;
using System.Web;
namespace WintonAuthorization
{
/// <summary>
/// 重現實現IHttpHandler
/// </summary>
public class AuthHandler:IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest reqest = context.Request;
//url请求
string fileName = string.Empty;
string urlStr = reqest.Url.AbsolutePath;
if (!string.IsNullOrEmpty(urlStr))
{
string[] urlArray = urlStr.Split('/');
fileName = urlArray[urlArray.Length - 1].ToUpper().Replace(".ASPX", "");
//取前8位
if (fileName.Length >= 8)
{
fileName = fileName.Substring(0, 8);
}
}
//是否存在文件
List<string> programList = context.Application["ProgramList"] as List<string>;
if (!string.IsNullOrEmpty(fileName) && null != programList && 0 < programList.Count && programList.Contains(fileName))
{
}
else
{
HttpResponse Response = context.Response;
Response.Write("<script >alert('該程式沒有授權!');history.back();</script>");
}
}
#endregion
}
}
Web.config中 配置:
<httpHandlers>
<add verb="*" path="*.aspx" type="WintonAuthorization.AuthHandler, WintonAuthorization" />
</httpHandlers>