locked
How to add header to WCF Service ? RRS feed

  • Question

  • I am using Visual Studio 2010. 
    To consume my WCF Service, I want users to send me login id and its password to some (most, but not all) method calls of my WCF Service.
    Instead of adding login id and its password to all the method signature, can I add it to the WCF Service header ?
    How can I do that ?

    If I use header, do all method needs pass that header to the WCF Service ? Some of the method doesn't need the login id and password.

    Thank you.        

    Monday, September 14, 2015 9:45 PM

Answers

All replies


  • Hi aujong,

    According to this case, in WCF you want some methods need pass

    the header to the service.

    You just need to write a function in the methods which has the

    soap header authentication

    via username and password. So, I  suggest you can use

    the inOperationContext.Current.IncomingMessageHeaders to get

    the Header's content in the service operations, and you can

    use  the  OperationContext.Current.OutgoingMessageHeaders  to

    adds a MessageHeader in the client.

    For more information, please refer to the following articles:

    1.OperationContext.IncomingMessageHeaders Property

    2.Adding Custom Message Headers to a WCF Service using Inspectors & Behaviors

    3.WCF Extensibility – Message Inspectors

    I hope that will be helpful to you.

    Best Regards,

    Grady


    • Edited by Grady_Dong Tuesday, September 15, 2015 5:38 AM
    • Proposed as answer by Grady_Dong Friday, September 18, 2015 9:04 AM
    • Marked as answer by Grady_Dong Monday, September 28, 2015 2:28 AM
    Tuesday, September 15, 2015 5:33 AM
  • Thank you.
    I added these codes to get the Account and Pwd from the SOAP Header
    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 == "Account")
                {
                    // Read the value of that header.
                    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
                    sAccount = xr.ReadElementContentAsString();
                }
                else if (h.Name == "Pwd")
                {
                    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
                    sPwd = xr.ReadElementContentAsString();
                }
            }
    
    In SOAPUI, I don't see "Account" and "Pwd" in the headers 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
       <soapenv:Header/>
       <soapenv:Body>
          <tem:GetInformation/>
       </soapenv:Body>
    </soapenv:Envelope>
    I need to add that manually (like below) before I submit the request to the WCF Service
     <soapenv:Header>
       <Account>123</Account>
       <Pwd>123password</Pwd>
       </soapenv:Header>
       <soapenv:Body>
          <tem:GetInformation/>
       </soapenv:Body>
    
    Is that correct ?
    If I submit the above headers "Account" and "Pwd" to the WCF Service, I am able to check those headers in the WCF Service.


    Tuesday, September 15, 2015 3:26 PM
  • Hi aujong,

    Yes that is  correctly. You need manually add the soapHeader in the client.

    Then you can check those headers in the WCF Service.

    Best Regards,

    Grady

    • Proposed as answer by Jimyken Monday, September 28, 2015 2:27 AM
    Wednesday, September 16, 2015 6:41 AM