У меня есть следующий код в C#
, который ищет apiKey
в следующем SOAP
заголовке:
Заголовок 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», что означает, что он не сможет получить значение.
Есть ли способ захватить <apiKey />
внутри <Authentication />
?
ОБНОВЛЕНИЕ 2:
Завершается с помощью следующего кода:
if (h.Name == "Authentication")
{
// Read the value of that header.
XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
xr.ReadToDescendant("apiKey");
apiKey = xr.ReadElementContentAsString();
}