【发布时间】: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”,这意味着它将无法检索该值。
有没有办法在<Authentication /> 中获取<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();
}
【问题讨论】:
-
然后返回什么?您是否调试过应用程序和 Web 服务?网络服务上是否有相同的数据?
-
尝试在某处记录 h.Name 的值
-
我已添加日志记录。请参阅上面的更新。
标签: c# wcf web-services soap