【问题标题】:Obtaining a value within a SOAP Header from the OperationContext从 OperationContext 获取 SOAP 标头中的值
【发布时间】:2011-06-21 07:33:14
【问题描述】:

我在C# 中有以下代码,它在以下SOAP 标头中查找apiKey

SOAP 标头:

<soap:Header>
   <Authentication>
       <apiKey>CCE4FB48-865D-4DCF-A091-6D4511F03B87</apiKey>
   </Authentication>
</soap:Header>

C#:

这是我目前所拥有的:

public string GetAPIKey(OperationContext operationContext)
{
    string apiKey = null;

    // Look at headers on incoming message.
    for (int i = 0; i < OperationContext.Current.IncomingMessageHeaders.Count; i++)
    {
        MessageHeaderInfo h = OperationContext.Current.IncomingMessageHeaders[i];

        // For any reference parameters with the correct name.
        if (h.Name == "apiKey")
        {
            // Read the value of that header.
            XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
            apiKey = xr.ReadElementContentAsString();
        }
    }

    // Return the API key (if present, null if not).
    return apiKey;
}

问题:返回 null 而不是实际的 apiKey 值:

CCE4FB48-865D-4DCF-A091-6D4511F03B87

更新 1:

我添加了一些日志记录。看起来h.Name 实际上是“身份验证”,这意味着它实际上不会寻找“apiKey”,这意味着它将无法检索该值。

有没有办法在&lt;Authentication /&gt; 中获取&lt;apiKey /&gt;

更新 2:

最终使用以下代码:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}

【问题讨论】:

  • 然后返回什么?您是否调试过应用程序和 Web 服务?网络服务上是否有相同的数据?
  • 尝试在某处记录 h.Name 的值
  • 我已添加日志记录。请参阅上面的更新。

标签: c# wcf web-services soap


【解决方案1】:

我认为您的 h.NameAuthentication 因为它是根类型,而 apiKey 是 Authentication 类型的属性。尝试将h.Name 的值记录到某个日志文件并检查它返回什么。

if (h.Name == "Authentication")
{
    // Read the value of that header.
    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
    //apiKey = xr.ReadElementContentAsString();
    xr.ReadToFollowing("Authentication");
    apiKey = xr.ReadElementContentAsString();
}

【讨论】:

  • 您将不得不使用其他东西,看起来您的 XmlReader 是空的,因为 OperationContext.Current.IncomingMessages 已经从 XmlReader 中读取了所有内容并且它是空的。 MessageHeaderInfo 类型有哪些可用的属性?当你做 h。 ,你在智能感知中看到了什么,你将不得不使用这些属性中的任何一个来获得所需的结果。
  • 我必须使用xr.ReadToDescendant("apiKey"); 才能使其正常工作。你带领我朝着正确的方向前进。谢谢。
【解决方案2】:

最终使用以下代码:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}

【讨论】:

    【解决方案3】:

    有一个更短的解决方案:

    public string GetAPIKey(OperationContext operationContext)
    {
        string apiKey = null;
        MessageHeaders headers = OperationContext.Current.RequestContext.RequestMessage.Headers;
    
        // Look at headers on incoming message.
        if (headers.FindHeader("apiKey","") > -1)
            apiKey = headers.GetHeader<string>(headers.FindHeader("apiKey",""));        
    
        // Return the API key (if present, null if not).
        return apiKey;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      相关资源
      最近更新 更多