我正在尝试使用第三方Web服务 https://staging.identitymanagement.lexisnexis.com/identity- proofing/services/identityProofingServiceWS/v2?wsdl
我已经将其添加为服务参考,但是我不确定如何传递标头的凭据。
如何使标头请求与此格式匹配?
<soapenv:Header> <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-49" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:Username>12345/userID</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/ oasis-200401-wss-username-token-profile-1.0#PasswordText">password123</wsse:Password> <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">d+VxCZX1cH/ieMkKEr/ofA==</wsse:Nonce> <wsu:Created>2012-08-04T20:25:04.038Z</wsu:Created> </wsse:UsernameToken> </wsse:Security> </soapenv:Header>
可能有一种更聪明的方法,但是您可以像这样手动添加标题:
var client = new IdentityProofingService.IdentityProofingWSClient(); using (new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageHeaders.Add( new SecurityHeader("UsernameToken-49", "12345/userID", "password123")); client.invokeIdentityService(new IdentityProofingRequest()); }
这SecurityHeader是一个自定义实现的类,由于我选择使用属性来配置XML序列化,因此还需要一些其他类:
SecurityHeader
public class SecurityHeader : MessageHeader { private readonly UsernameToken _usernameToken; public SecurityHeader(string id, string username, string password) { _usernameToken = new UsernameToken(id, username, password); } 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"; } } protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) { XmlSerializer serializer = new XmlSerializer(typeof(UsernameToken)); serializer.Serialize(writer, _usernameToken); } } [XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] public class UsernameToken { public UsernameToken() { } public UsernameToken(string id, string username, string password) { Id = id; Username = username; Password = new Password() {Value = password}; } [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")] public string Id { get; set; } [XmlElement] public string Username { get; set; } [XmlElement] public Password Password { get; set; } } public class Password { public Password() { Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; } [XmlAttribute] public string Type { get; set; } [XmlText] public string Value { get; set; } }
我没有Nonce在UsernameTokenXML中添加该位,但是它与XML非常相似Password。该Created元素还需要还增加,但它是一个简单的[XmlElement]。
Nonce
UsernameToken
Password
Created
[XmlElement]