WCF Header Manipulation

I am creating a client for some STS service, and for more than one day now I have been trying to add a header to a WCF message. In my RequestSecurityToken request, I must specify a UsernameToken.

I am not sure how to do this. At the moment, I determined the behavior of the endpoint and the message inspector (took me long enough to detect them ...). In BeforeSendRequest () of the latter, I create an object of the custom class "Security", which comes from MessageHeader. Security includes an instance of UsernameToken.

public class MessageInspector : IClientMessageInspector {

 public object BeforeSendRequest(ref Message request, IClientChannel channel) {
    Security uns = new Security();
    uns.UsernameToken = new UsernameToken();

    // ...

    var Header = new MessageHeader<Security>(uns);
    var untyped = Header.GetUntypedHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    request.Headers.Add(untyped);
    return null;
 }
}

public class Security : MessageHeader {
 public UsernameToken UsernameToken = new UsernameToken();

 public override string Name {
    get { return "Security"; }
 }

 public override string Namespace {
    get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
 }
}

public class UsernameToken {
 public String Username = "";
 public Password Password = new Password();
}

This is what is serialized.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:RequestSecurityToken</Action>
    <Security xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <UsernameToken xmlns="http://schemas.datacontract.org/2004/07/Tarifrechner.Kfz">
        <Password>
          <Type>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText</Type>
          <password>******</password>
        </Password>
        <Username>******</Username>
      </UsernameToken>
    </Security>
  </s:Header>
  <s:Body />
</s:Envelope>

In particular, the UsernameToken namespace seems to be incorrect. I know this comes from serializing data contracts, but I need a different namespace.

Here is what I would like serialized data to look like

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="...">
  <soap:Header>
    <Security xmlns:q1="http://www.bipro.net/namespace" xsi:type="q1:UserNameSecurity" 
          xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <UsernameToken>
        <Username>******</Username>
        <Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">******</Password>
      </UsernameToken>
    </Security>
    <wsa:Action>urn:RequestSecurityToken</wsa:Action>
    <wsse:Security>
      <wsu:Timestamp wsu:Id="Timestamp-b9dd599d-5901-451d-8321-6a309091f273">
        <wsu:Created>2012-03-11T16:02:56Z</wsu:Created>
        <wsu:Expires>2012-03-11T16:07:56Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <RequestSecurityToken xmlns="http://schemas.xmlsoap.org/ws/2005/02/trust">
      <TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</TokenType>
      <RequestType>
        http://schemas.xmlsoap.org/ws/2005/02/trust/Issue
      </RequestType>
    </RequestSecurityToken>
  </soap:Body>
</soap:Envelope>

? , , ?


, , UsernameToken. , WCF .

, WS2007HttpBinding, SecurityMode.TransportWithMessageCredential InstallSecurityContext, false, XML, . ?

: body, , , RequestSecurityToken body. - , ?

CreateSecurityContext = true , "urn: RequestSecurityToken" "http://docs.oasis-open.org/ws-sx/ws- /200512/RST/SCT".


!

!

+5
2

MessageContract , , SOAP, . , :

[ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        MyResponse DoSomething(MyRequest request);
    }

    public class MyService : IMyService
    {
        public MyResponse DoSomething(MyRequest request)
        {
            return new MyResponse()
            {
                Details = "Service did something awesome.",
                Timestamp = DateTime.Now
            };
        }
    }

    [MessageContract(IsWrapped = true, WrapperNamespace = "http://myservice/messages/")]
    public class MyRequest
    {
        [MessageHeader(Namespace = "http://myservice/security")]
        public string TokenThingy
        {
            get;
            set;
        }
    }

    [MessageContract(IsWrapped = true, WrapperNamespace = "http://myservice/messages")]
    public class MyResponse
    {
        [MessageBodyMember]
        public string Details
        {
            get;
            set;
        }

        [MessageBodyMember]
        public DateTime Timestamp
        {
            get;
            set;
        }
    }

SOAP:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IMyService/DoSomething</Action>
    <h:TokenThingy xmlns:h="http://myservice/security">fda</h:TokenThingy>
  </s:Header>
  <s:Body>
    <MyRequest xmlns="http://myservice/messages/" />
  </s:Body>
</s:Envelope>

:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <MyResponse xmlns="http://myservice/messages">
      <Details xmlns="http://tempuri.org/">Service did something awesome.</Details>
      <Timestamp xmlns="http://tempuri.org/">2012-05-04T17:04:36.5980424-04:00</Timestamp>
    </MyResponse>
  </s:Body>
</s:Envelope>
+1

, .

OperationContextScope , . OperationContextScope OperationContext.Current, OutgoingMessageHeaders. . , .

( - WCF ) :

0

All Articles