如何在 web.config 文件中为 C# 应用程序创建规则,以根据 URL 中的参数进行阻止
How to create a rule in a web.config file for a C# application, to block based on parameters in the URL
我想在 web.config 文件中为 C# 应用程序创建过滤规则,以阻止 http://website.com/folder/Default.aspx,
但允许 http://website.com/folder/Default.aspx?db=Database。基本上他们在 URL 中没有 "Default.aspx?db=Database",然后它将把他们重定向到另一个网站(或拒绝访问)。
您可以实现一个执行所需逻辑的 HttpModule。您可以在 web.config 中注册它,而无需对项目进行任何代码更改。只需将其编译成自己的 dll 并将其放入您网站的 bin 文件夹即可。
模块
class RequestInterceptor : IHttpModule
{
public void Dispose()
{
}
// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source,
EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
//Inspect the URL and decide if this is a request you are interested in
//context.Request.Url
//context.Request.QueryString
//Redirect, or whatever...
//context.Response.Redirect(...)
}
}
web.config:
<system.webServer>
<modules>
<add name="MyInterceptor" type="YourNamespace.RequestInterceptor, RequestInterceptor"/>
</modules>
...
我想在 web.config 文件中为 C# 应用程序创建过滤规则,以阻止 http://website.com/folder/Default.aspx, 但允许 http://website.com/folder/Default.aspx?db=Database。基本上他们在 URL 中没有 "Default.aspx?db=Database",然后它将把他们重定向到另一个网站(或拒绝访问)。
您可以实现一个执行所需逻辑的 HttpModule。您可以在 web.config 中注册它,而无需对项目进行任何代码更改。只需将其编译成自己的 dll 并将其放入您网站的 bin 文件夹即可。
模块
class RequestInterceptor : IHttpModule
{
public void Dispose()
{
}
// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source,
EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
//Inspect the URL and decide if this is a request you are interested in
//context.Request.Url
//context.Request.QueryString
//Redirect, or whatever...
//context.Response.Redirect(...)
}
}
web.config:
<system.webServer>
<modules>
<add name="MyInterceptor" type="YourNamespace.RequestInterceptor, RequestInterceptor"/>
</modules>
...