我正在努力與WCF
綁定配置。我有第三方服務添加爲我的項目的參考。我被提供了一些規範。WSHttpBinding TransportWithMessageCredential SecurityMode改變請求
我需要使用SOAP v1.2
所以我想,我需要WSHttpBinding
這將是https
所以我需要SecurityMode.Transport
事情是這樣的:
var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
var client = new MyClient(binding, addr);
var result = client.Method(new MyObject());
它導致此請求主體。
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<S:Header>
<wsa:MessageID>
uuid:6B29FC40-CA47-1067-B31D-00DD010662DA
</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>example</wsa:Address>
</wsa:ReplyTo>
<wsa:To>example</wsa:To>
<wsa:Action>example</wsa:Action>
</S:Header>
<S:Body>
<MyObject>...</MyObject>
</S:Body>
</S:Envelope>
根據我所提供的說明書中,我需要包括在報頭Security
元件與UsernameToken
和Timestamp
。究竟是什麼我會BasicHttpBinding
和BasicHttpSecurityMode.TransportWithMessageCredential
獲得當我嘗試設置TransportWithMessageCredential
模式WSHttpBinding
請求主體急劇變化。
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
var client = new MyClient(binding, addr);
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "123456";
var result = client.Method(new MyObject());
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<S:Header>
<wsa:MessageID>
uuid:b633067e-9a9e-4216-b036-4afa3aca161e
</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:To>example</wsa:To>
<wsa:Action>http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT</wsa:Action>
<o:Security s:mustUnderstand=1 xmlns:o=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd>
<u:Timestamp>...<u:Timestamp>
<o:UsernameToken>
<o:Username>...</o:Username>
<o:Password>...</o:Password>
</o:UsernameToken>
</o:Security>
</S:Header>
<S:Body>
<t:RequestSecurityToken>...</t:RequestSecurityToken>
</S:Body>
</S:Envelope>
現在我有安全部分正確的,但所有的尋址部分是錯誤的,身體也是如此。我該怎麼辦?
我期待(我需要)是這樣的:
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<S:Header>
<wsa:MessageID>
uuid:6B29FC40-CA47-1067-B31D-00DD010662DA
</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>example</wsa:Address>
</wsa:ReplyTo>
<wsa:To>example</wsa:To>
<wsa:Action>example</wsa:Action>
<o:Security s:mustUnderstand=1 xmlns:o=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd>
<u:Timestamp>...<u:Timestamp>
<o:UsernameToken>
<o:Username>...</o:Username>
<o:Password>...</o:Password>
</o:UsernameToken>
</o:Security>
</S:Header>
<S:Body>
<MyObject>...</MyObject>
</S:Body>
</S:Envelope>
我建議你嘗試的步驟在那裏http://stackoverflow.com/questions/25776403/wcf-create-usernametoken-with-timestamp-and-password-digest-for-oasis-200401-w – mfatih