Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.ServiceModel;
Using System.ServiceModel.Channels;
Using System.Web;
Using System.Xml;
Using System.Xml.Serialization;
Namespace WebApplication1
{
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;}
}
}
Call
void Test () {
Servicereference1.recvcustomer rc = new Servicereference1.recvcustomer
{
Reqinfo = "123",
Reqdata = "456"
};
Servicereference1.ws_esb_customerrecvporttypeclient client = new Servicereference1.ws_esb_ Customerrecvporttypeclient ();
using (new OperationContextScope (client). Innerchannel))
{
OPERATIONCONTEXT.CURRENT.OUTGOINGMESSAGEHEADERS.ADD (
New Securityheader ("UsernameToken-49", "username", "password"));
var ss= client.recvcustomer (RC);
}
}
The second method:
Web. config Direct configuration
<client>
<endpoint address= "Http://61.191.200.162:5888/services/WS_ESB_CustomerRecv"
binding= "CustomBinding" bindingconfiguration= "ws_esb_customerrecvsoap12binding"
contract= "Servicereference1.ws_esb_customerrecvporttype" name= "Ws_esb_customerrecvhttpsoap12endpoint" >
<wsse:security xmlns:wsse= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ">
<wsse:UsernameToken>
<wsse:Username>UserName</wsse:Username>
<wsse:password type= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0# Passwordtext ">wsse:[email protected]</wsse:password>
</wsse:UsernameToken>
</wsse:Security>
</endpoint>
</client>
C # Call WebService error An error is discovered processing the <wsse:Security> header