【问题标题】:How do SOAP headers work in ASMX Web Services?SOAP 标头在 ASMX Web 服务中如何工作?
【发布时间】:2011-05-04 16:57:55
【问题描述】:

我有一个项目使用带有 [WebMethods] 和 [SoapHeader("Auth")] 的 asmx 文件。我没有使用 SOAP 的经验,也不明白它是如何工作的。

通过代码,我注意到您可以使用与标题同名的变量,它包含数据。数据如何发送到标头?它来自哪里?

【问题讨论】:

    标签: asp.net web-services soap asmx


    【解决方案1】:

    数据通过使用从 SoapHeader 派生的类在标头中发送。此类将被声明为您的 Web 服务类中的属性。然后在您的 web 方法中,您将在处理实际方法之前检查此属性中的身份验证信息。

    一个简单的实现可以在这里找到http://www.codeproject.com/KB/cpp/authforwebservices.aspx

    下面的 msdn 链接讲述了另一种类似的技术,它会更复杂 http://msdn.microsoft.com/en-us/library/9z52by6a.aspx

    在标头中传递数据的基本思想保持不变。

    【讨论】:

      【解决方案2】:

      数据来自 SOAP 信封的 <soap:Header> 部分中的 XML。

      【讨论】:

      • 所以有些东西正在发送一个 SOAP 请求并且该属性获取了标头?对吗?
      • 或多或少。可能有多个标题。只会抓取匹配的标题。
      【解决方案3】:

      像往常一样为您的肥皂标题创建一个类。

      public class AuthHeader : SoapHeader
      {
          public string CompanyID;
          public string Username;
          public string Password;
      }
      

      然后在你的普通班级中有参考。

      public class MyClass : WebService
      {
          public readonly AuthHeader authHeader;
      
          [SoapHeader("authHeader", Direction = SoapHeaderDirection.In)]
          [WebMethod(CacheDuration = 20
              , EnableSession = true
              , Description = "Find stuff now."
              , MessageName = "FindStuff")]
          [ScriptMethod(UseHttpGet = false
              , ResponseFormat = ResponseFormat.Xml
              , XmlSerializeString = true)]
      
          public MyResponseClass FindStuff(string searchString)
          {
              MyResponseClass myResponseClass = new MyResponseClass();
              if (authHeader.Username == "myUser" &&
                  authHeader.Password == "myPass" &&
                  authHeader.CompanyID == "BobsTire")
              {
                  ....
                  myResponseClass = ....
              }
              return myResponseClass;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多