【问题标题】:Determine if a HTTP request is a soap request on HttpApplication.AuthenticateRequest确定 HTTP 请求是否是 HttpApplication.AuthenticateRequest 上的肥皂请求
【发布时间】:2011-01-07 16:55:18
【问题描述】:

我有办法知道请求是否是 HttpApplication 的 AuthenticateRequest 事件上的肥皂请求?检查 ServerVariables["HTTP_SOAPACTION"] 似乎并非一直有效。

public void Init(HttpApplication context) {
    context.AuthenticateRequest += new EventHandler(AuthenticateRequest);
}

protected void AuthenticateRequest(object sender, EventArgs e) {
    app = sender as HttpApplication;
    if (app.Request.ServerVariables["HTTP_SOAPACTION"] != null) {
        // a few requests do not enter here, but my webservice class still executing
        // ...
    }
}

我在我的 web.config 文件中禁用了 Web 服务的 HTTP POST 和 HTTP GET。

<webServices>
    <protocols>
      <remove name="HttpGet" />
           <remove name="HttpPost" />
      <add name="AnyHttpSoap" />
    </protocols>
</webServices>

查看soap+xml 的ContentType 只能部分解决我的问题。例如,

Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 1131
Content-Type: text/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: ro
Host: localhost
mymethod: urn:http://www.wsnamespace.com/myservice

有些客户端没有标准头 SOAPAction:“http://www.wsnamespace.com/myservice/mymethod”,而是有一些类似于上面示例的内容。 “mymethod”表示我的 Web 服务类中带有 [WebMethod] 属性的方法,“http://www.wsnamespace.com/myservice”是 Web 服务的命名空间。服务仍然完全正常。 消费者使用不同的框架(来自 PHP、.NET、Java 等的 NuSOAP)。

【问题讨论】:

    标签: asp.net web-services asmx


    【解决方案1】:

    您可以查看Request.ContentType 属性,如果客户端正确设置该属性应该是

    application/soap+xml; charset=utf-8
    

    utf-8 部分可能不存在。

    除此之外,您当然可以只检查 URL,如果它是一个网络服务,那么它会告诉您它是什么。

    【讨论】:

    • 我还想在 Web 服务调用和它自动生成的 html 文档(它具有相同的 url)之间有所区别。也许在我的情况下,我可以检查 .asmx 扩​​展名,然后检查任何 text/xml、application/xml、aplication/soap+xml 等的 ContentType。
    【解决方案2】:

    我总是为 Web 服务提供自己的端口。这样我就不必过滤通过端口 80 的每个 HTTP 请求。或者更确切地说,我可以过滤端口 80 以解决面向浏览器的问题,并过滤 SOAP/SOA 端口以解决其他类型的攻击。

    IMAO,将(可能)敏感的业务数据与公共数据混合在一起,这样您就不必在防火墙上打开另一个漏洞,这就是您一开始就有防火墙的原因。

    【讨论】:

      【解决方案3】:

      您也可以走更难的路线,并根据 HTTP 标头下的所有其他内容来解决问题。我的意思是,分析如下内容,即 SOAP 请求正文 - 请求的一部分...

      <soap:Envelope xmlns:soap="..." soap:encodingStyle="...">
      

      IBM

      【讨论】:

        【解决方案4】:

        您是否测试过 System.Web.HttpContext.Current.Request.CurrentExecutionFilePathExtension ? 通常这将是 .asmx 用于 web 服务(json 和 xml),当然只要你处理服务。

        【讨论】:

          【解决方案5】:

          我正在使用以下代码来识别请求类型。如果它符合您的要求,请尝试此操作。如果对您有帮助,请标记为答案。

          if (request.Headers["SOAPAction"] != null || request.ContentType.StartsWith("application/soap+xml"))
              return ServiceRequestTypes.SoapRequest;
          else if ("POST".Equals(request.RequestType, StringComparison.InvariantCultureIgnoreCase) && request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.InvariantCultureIgnoreCase))
              return ServiceRequestTypes.HttpPostRequest;
          else if ("POST".Equals(request.RequestType, StringComparison.InvariantCultureIgnoreCase) && request.ContentType.StartsWith("application/json", StringComparison.InvariantCultureIgnoreCase))
              return ServiceRequestTypes.AjaxScriptServiceRequest;
          return ServiceRequestTypes.Unknown;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-10
            • 1970-01-01
            • 2020-07-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多