1 year ago
#355017
codinges
What is the best way to send usernametoken in header in soap request?
public async Task GetUserInfoAsync(){
TestClient client = new TestClient();
client.Endpoint.EndpointBehaviors.Add(new WsSecurityEndpointBehavior("test", "12345"));
client.GetUserInfo(new GetUserInfoRequest{
token = "866-563-6746"
})
}
public class WsSecurityEndpointBehavior : IEndpointBehavior
{
private readonly string _username;
private readonly string _password;
public WsSecurityEndpointBehavior(string username, string password)
{
_username = username;
_password = password;
}
public void AddBindingParameters(ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new WsSecurityMessageInspector(_username, _password));
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
public class WsSecurityMessageInspector : IClientMessageInspector
{
private readonly string _username;
private readonly string _password;
public WsSecurityMessageInspector(string username, string password)
{
_username = username;
_password = password;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var header = new Security
{
UsernameToken =
{
Password = new Password
{
Value = _password,
Type =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"
},
Username = _username
}
};
request.Headers.Add(header);
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
public class Password
{
[XmlAttribute] public string Type { get; set; }
[XmlText] public string Value { get; set; }
}
[XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken
{
[XmlElement] public string Username { get; set; }
[XmlElement] public Password Password { get; set; }
}
public class Security : MessageHeader
{
public Security()
{
UsernameToken = new UsernameToken();
}
public UsernameToken UsernameToken { get; set; }
public override string Name => GetType().Name;
public override string Namespace =>
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public override bool MustUnderstand => true;
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
var serializer = new XmlSerializer(typeof(UsernameToken));
serializer.Serialize(writer, UsernameToken);
}
}
I successfully add UsernameToken to the header with the username and password parameters I sent in GetUserInfoAsync in the above structure. With this structure, I can get the following xml output.
But I also think there is a better way than this. Here I want to set up a dynamic structure that I can use in every soap request. What is the best way to do this?
<soapenv:Envelope xmlns:pos="http://test.test.com/xmlschema/pos" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<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" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurityutility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>test</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile1.0#PasswordText">12345</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<pos:GetUserInfoRequest>
<pos:token>866-563-6746</pos:token>
</pos:GetUserInfoRequest>
</soapenv:Body></soapenv:Envelope>
c#
.net
soap
soapheader
usernametoken
0 Answers
Your Answer