覆盖Request.QueryString [英] override Request.QueryString

查看:93
本文介绍了覆盖Request.QueryString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,经验丰富的开发人员!

Hello fellow seasoned developers!

我想知道是否有可能以某种方式覆盖.Net Request.QueryString对象?如果可以在不必创建我自己的HTTP模块的情况下完成此操作,那将非常好.

I was wondering if it were possible to override the .Net Request.QueryString object somehow? It would be really nice if this could be done without having to create my own HTTP module as well.

我的任务是使用RSA(1024)对应用程序中的所有查询字符串进行加密.但是,该应用程序已经构建完毕,并且在很多地方都设置了查询字符串,因此理想情况下,我想进行全局更改以解密该查询并将其放置在普通的Request.QueryString中,从而不必更改我的到处都可以编写代码,也可以将其传递给我们团队中的其他开发人员,他们也不必更改其代码.

I have been tasked with RSA(1024) encrypting all the querystrings in my application. However, the application is already built and there's a LOT of places where querystrings are being set so ideally i would like to make a global change that would decrypt the query and place it in the normal Request.QueryString so as to not have to change my code everywhere, and maybe pass it along to other devs within my team and they also don't have to change their code.

现在,我已经构建了加密对象,并使用SessionID作为盐,以使每个会话的密钥唯一.我还尝试在Global.asax中拦截HTTP请求,以便使用解密后的查询重写请求路径,但是,由于在这些页面上执行的任何回发操作都将解密后的查询字符串放回POST中,所以这很失败.不想.

Now, I already built the encryption object and use the SessionID for salts to make the keys unique per session. I have also tried intercepting the HTTP request in Global.asax so as to rewrite the request path with the decrypted query, however, that was a bust as any postbacks being performed on those pages put the decrypted querystring back into the POST, which i obviously don't want.

因此,现在我正处于一个阶段,在此阶段,我希望而不是重新编写路径,而是在全局级别上拦截或覆盖Request.QueryString对象,并在每次对此this [key]进行调用时在其中使用我的解密方法.被放置,因此,不必停止使用Request.QueryString.但是,在网上搜索了数小时之后,我找不到有关如何执行此操作的单个示例...

So now i'm at a stage where i would like instead of re-writing the path, to intercept or override the Request.QueryString object on a global level and use my decryption methods there whenever a call to this[key] is placed, and thus again, not having to stop using Request.QueryString. However, after hours of searching on the web, i could not find a single example on how to do this...

如果有人能帮助我解决这个问题,我将不胜感激!

If anyone could help me out with this i would be most grateful!

推荐答案

我认为最简单的方法是使用扩展方法.也许是这样的:

I think the easiest way to accomplish this is to use an Extension Method. Perhaps something like this:

    class Program 
{
    static void Main() 
    {
        var decryptedValue = HttpContext.Current.Request.DecryptQueryStringParam("myParam");
    }
}

public static class HttpRequestExtensions 
{
    public static string DecryptQueryStringParam(this HttpRequest extendee, string name)
    {
        // do stuff to decrypt
        return DecryptMethodStub(extendee.QueryString[name]);
    }

    private string DecryptMethodStub(string queryString)
    {
        return "something decrypted the string";
    }
}

请注意,上面的Program类仅用于说明目的……实际上,您将在asp.net Web表单页面或MVC控制器的正文中调用Request.{ExtensionMethod},这些控件已经可以直接访问通过Request属性的HttpRequest对象.

Please note that the Program class above is for illustrative purposes only... in reality you would call Request.{ExtensionMethod} within the body of a asp.net web forms page or an MVC controller which already provide direct access to the HttpRequest object through the Request property.

以下是有关扩展程序的一些信息:

here is some information about extensions:

http://msdn.microsoft.com/en-us/library/bb383977.aspx

这篇关于覆盖Request.QueryString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆