【问题标题】:HttpContext, alternative approach to Context.Request.Unvalidated for pre .Net 4.5 serversHttpContext,用于 .Net 4.5 之前的服务器的 Context.Request.Unvalidated 的替代方法
【发布时间】:2014-06-23 22:04:15
【问题描述】:

我的项目中有一个方法,如下所示,它接受一个HttpContext 对象并返回一个string

//returns the xml document as string
private static string GetXmlReceiptFromContext(HttpContext context)
{
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.Cache.SetNoStore();
    context.Response.Cache.SetExpires(DateTime.MinValue);        
    return context.Request.Unvalidated.Form.Get("status");
}

这个结果最终被传递给一个非常重要的方法,需要这个字符串。

Context.Request.Unvalidated 似乎只能从 .Net 4.5 获得。 对于没有 .Net 4.5 并且将使用此程序集的服务器,我需要使用此方法的替代方法。

谁能建议一种替代方法来访问和返回状态参数,谁的值将是上下文中的 XML 文档,而不使用 Context.Request.Unvalidated?

编辑

这不适用于 webform 或 MVC 项目,我们开发了一个类库,我们理想地希望在程序集中包含所有与支付相关的功能,即单一职责,我们将使用它的前端应用程序不需要了解付款方面的事情。

【问题讨论】:

  • @generalexception 我会非常小心地使用它。它禁用整个应用程序的请求验证,这可能不是 SelectDistinct 所追求的。
  • @martennis 如果你向下滚动页面,我想你会发现你可以只禁用一个页面!!
  • 您使用的是哪个框架?他们都有不同的过滤器来“保护”你。
  • 我们需要对此进行编码以使用 4.0

标签: c# .net


【解决方案1】:

我不知道您使用的是 MVC 还是 Web 表单/网页,但有几种解决方案可供您使用。查看this MSDN page 了解有关禁用请求验证的更多信息。

Web 表单:将 <@ Page validateRequest="false" %> 添加到页面顶部以禁用对单个页面的验证(更多页面/应用程序部分的其他选项在 MSDN 页面中)。

MVC:将[ValidateInput(false)] 属性添加到您的操作顶部或将[AllowHtml] 属性添加到您要绑定到的模型中的属性。

【讨论】:

  • 除非您在 web.config 中设置 requestvalidationmode="2.0",否则这将不起作用。
  • @generalexception 谢谢你,我误读了这篇文章 :)
  • @martennis 感谢您的回答,这确实可行,但是我们需要将其降低一个级别,因为我们必须将其包含在类库中并且不希望将其实现到每个 Web 应用程序中它利用了我们的图书馆。查看问题更新
【解决方案2】:

您可以使用BinaryRead 并自己解析原始请求正文。请参阅下面的 RawPostValues() 方法。

public class Handler1 : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(GetXmlReceiptFromContext(context));
        context.Response.Write("</ br>");
        context.Response.Write(GetXmlReceiptFromContext(context));
    }

    //returns the xml document as string
    private static string GetXmlReceiptFromContext(HttpContext context)
    {
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetExpires(DateTime.MinValue);
        return RawPostValues(context)
            .SingleOrDefault(kvp => kvp.Key.Equals("status", StringComparison.InvariantCultureIgnoreCase))
            .Value;
    }

    private static IEnumerable<KeyValuePair<string, string>> RawPostValues(HttpContext context)
    {
        if (context.Request.HttpMethod != "POST") yield break;

        var tmpPosition = context.Request.InputStream.Position;
        string[] formElements;

        try
        {
            formElements = System.Text.Encoding.Default.GetString(
                context.Request.BinaryRead(context.Request.ContentLength))
                .Split('&');

            if (formElements.Length < 1) yield break;
        }
        finally
        {
            context.Request.InputStream.Position = tmpPosition;
        }

        foreach (var element in formElements)
        {
            if (string.IsNullOrEmpty(element)) continue;

            var key = element.Substring(0, element.IndexOf('='));
            var value = element.Substring(key.Length + 1, element.Length - key.Length - 1);
            yield return new KeyValuePair<string, string>(key, value);
        }
    }
}

使用 Fiddler2 进行的一些快速冒烟测试表明这是可行的。您可能需要解决一些问题。

请求

POST http://localhost:48707/Handler1.ashx HTTP/1.1
Host: localhost:48707
Content-Length: 19
content-type: application/x-www-form-urlencoded

status=<b>oops!</b>

回应

HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Thu, 22 May 2014 05:29:26 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Connection: Close

<b>oops!</b>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多