【问题标题】:Validating Requests for BundleConfig Routes/URLs验证 BundleConfig 路由/URL 的请求
【发布时间】:2017-03-13 10:13:50
【问题描述】:

在 ASP.NET 中是否有办法验证在使用 BundleConfig ScriptBundle 创建的虚拟路径/路由中发送的请求参数?

例子:

我有一个 BundleConfig 配置如下:

bundles.Add(new ScriptBundle("~/bundles/bjqs").Include(
                "~/Scripts/bjqs-1.3.js"));

如果用户发送如下请求:

http://example.com/bundles/bjqs?v=parameter-value_to%be+validated

在 ASP.NET 处理/处理请求之前,如何针对正则表达式验证查询字符串 v 参数中传递的值?

【问题讨论】:

    标签: c# asp.net security bundling-and-minification


    【解决方案1】:

    您可以使用自定义HttpModule 来拦截请求。例如:

    public class MyModule1 : IHttpModule
    {
        public void Dispose() {}
    
        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += context_AuthorizeRequest;
        }
    
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
            var app = (HttpApplication)sender;
    
            // Check if the parameter is valid, your logic
            if (ValidateRequest(app.Context.Request))
            {
                // Then do nothing
                return;
            }
    
            // Otherwise, return unauthorized response
            app.Context.Response.StatusCode = 401;
            app.Context.Response.End();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-13
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多