Nic*_*len 5 asp.net sanitization query-string
我们通过电子邮件向客户发送注册网址.一些电子邮件客户端正在将网址转换为
url <url>
Run Code Online (Sandbox Code Playgroud)
我认为当用户将电子邮件转发给自己时,可能会发生这种情况,此时电子邮件客户端会重新格式化原始电子邮件(可能)
例如
https://my.app.com/login.aspx?param=var
变
https://my.app.com/login.aspx?param=var%20%3Chttps://my.app.com/login.aspx?param=var%3E
哪个正确生成System.Web.HttpRequestValidationException:检测到一个潜在危险的Request.QueryString值
我应该在代码中拦截这些实例并对网址进行消息处理,以便将用户重定向到网址的原始格式?
Global.asax中?Page_Init?HttpHandler的?管道?
您可以在 Global Application_BeginRequest 或 HttpModule 中的同一事件中捕获它。
全球的
using System;
using System.Web;
namespace MassageIncomingRequestUrl
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication) sender;
string path = app.Context.Request.Url.PathAndQuery;
int pos = path.IndexOf("%20%3C");
if (pos > -1)
{
path = path.Substring(0, pos);
app.Context.RewritePath(path);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
模块
using System;
using System.Web;
namespace MassageIncomingRequestUrl
{
public class UrlMungeModule : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
}
public void Dispose()
{
//nop
}
#endregion
private static void BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
string path = app.Context.Request.Url.PathAndQuery;
int pos = path.IndexOf("%20%3C");
if (pos>-1)
{
path = path.Substring(0,pos);
app.Context.RewritePath(path);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论您在浏览器地址中看到什么,这都会使用请求中的正确查询字符串处理您的请求。您也许可以采取额外的步骤来删除报告的网址中的垃圾,但这主要只是为了美观。
| 归档时间: |
|
| 查看次数: |
5193 次 |
| 最近记录: |