惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Martin Fowler
Martin Fowler
H
Help Net Security
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 最新话题
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
The Hacker News
The Hacker News
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
SecWiki News
SecWiki News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
Attack and Defense Labs
Attack and Defense Labs
Security Latest
Security Latest
Help Net Security
Help Net Security
博客园 - Franky
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
Cloudbric
Cloudbric
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 【当耐特】
Schneier on Security
Schneier on Security
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Webroot Blog
Webroot Blog
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
O
OpenAI News
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog

博客园 - Leon0812

Export csv Export Excel Jquery pluign development example XML Mail Template Management Resource file management Excel Reader iframe 中显示内嵌页的局部 - Leon0812 - 博客园 easyslider - Leon0812 - 博客园 缩放页面字体 可编辑DIV - Leon0812 - 博客园 vs快捷键大全(转) SQL文件執行 C#调用非托管的DLL 无缝滚动 - Leon0812 - 博客园 获取-编译-打包 VS作业一条龙批处理 Reporting Service for SQL 2008匿名访问报表方法 獲取控件位置 - Leon0812 - 博客园 Installscript中修改web.config - Leon0812 - 博客园 publish
利用IHttpModule、IHttpHandler做授权类 - Leon0812 - 博客园
Leon0812 · 2010-02-23 · via 博客园 - Leon0812

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>