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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
量子位
博客园_首页
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Forbes - Security
Forbes - Security
IT之家
IT之家
N
News and Events Feed by Topic
S
Security Affairs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
Recorded Future
Recorded Future
L
LangChain Blog
Y
Y Combinator Blog
AI
AI
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google Online Security Blog
Google Online Security Blog
V2EX - 技术
V2EX - 技术
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
I
Intezer
T
Tenable Blog
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
O
OpenAI News
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
S
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Blog — PlanetScale
Blog — PlanetScale

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