开发者

WCF set custom header - reading not working

I need to put custom headers into WCF. My Code is as follows:

ServiceReference1.Service2Client ws = new Service2Client();
   using (OperationContextScope scope = new OperationContextScope((IContextChannel)ws.InnerChannel))
        {
            MessageHeaders messageHeadersElement = OperationContext.Current.OutgoingMessageHeaders;
            messageHeadersElement.Add(MessageHeader.CreateHeader("Authorization", String.Empty, "string"));
            messageHeadersElement.Add(MessageHeader.CreateHeader("username", String.Empty, "user"));
            var res = ws.GetUser("123");
        }
开发者_StackOverflow中文版

But when I try to read it in the service, nothing is availabe in the following

public class OAuthAuthorizationManager : ServiceAuthorizationManager
    {
        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("username", String.Empty);

            string auth = operationContext.IncomingMessageHeaders.GetHeader<string>("username", String.Empty);

            var hereIseeIt = operationContext.RequestContext.RequestMessage;

index is -1: not found

auth: is also displaying an exception that the header is not available

hereIseeIt: .ToString() shows a xml where I can see that user is existent, but I see no way to access that information in any of the objects

WCF set custom header - reading not working

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <username xmlns="http://Microsoft.WCF.Documentation">user</username>
  </s:Header>
  <s:Body>
    <GetUser xmlns="http://tempuri.org/">
      <UserId>123</UserId>
    </GetUser>
  </s:Body>
</s:Envelope>

But I cannot access them since I find no way to access the s:Header ...


try using:

XPathNavigator XPN = operationContext.RequestContext.RequestMessage.CreateBufferedCopy ().CreateNavigator ();

NOT elegant but it gives you the whole Message accessible through a XPathNavigator which should make it easy to get to any value inside the Message you want..

some links:

  • http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.requestcontext.aspx
  • http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.aspx
  • http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.aspx


Here's an easy way to get the inner XML of the username header for your scenario. Even if you already solved your issue a long time ago, I thought it might help somebody else who faces the same issue.

var username = String.Empty;

// using the namespace from you XML sample
var usernameHeaderPosition = OperationContext.Current
    .IncomingMessageHeaders
    .FindHeader("username", "http://Microsoft.WCF.Documentation");

if (usernameHeaderPosition > -1)
{
    username = OperationContext.Current
        .IncomingMessageHeaders
        .GetReaderAtHeader(usernameHeaderPosition).ReadInnerXml();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜