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

推荐订阅源

AWS News Blog
AWS News Blog
T
Tenable Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
Threatpost
Security Latest
Security Latest
C
Cisco Blogs
L
Lohrmann on Cybersecurity
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
AI
AI
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Hacker News: Front Page
U
Unit 42
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MyScale Blog
MyScale Blog
O
OpenAI News
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cyberwarzone
Cyberwarzone
博客园 - 【当耐特】
H
Heimdal Security Blog
S
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
Help Net Security
Help Net Security
D
DataBreaches.Net
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
TaoSecurity Blog
TaoSecurity Blog
K
Kaspersky official blog
N
News and Events Feed by Topic
WordPress大学
WordPress大学
P
Palo Alto Networks Blog

博客园 - 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>