如何在c#中添加头部肥皂请求

时间:2012-05-03 08:34:46

标签: c# soap header wsse

标题对象

[Serializable]
[DataContract(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] // This object serialize specific namespace
public class Security
{
    [DataMember] // This object serialize without namespace
    public UsernameToken UsernameToken;
}


public class UsernameToken : IXmlSerializable
{

    public string Username { get; set; }
    public string Password { get; set; }

    public XmlSchema GetSchema() { return null; }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.MoveToContent();

        Username = reader.ReadElementString("Username");
        reader.ReadStartElement();

        Password = reader.ReadElementString("Password");
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Username", Username);
        writer.WriteElementString("Password", Password);
    }
}

设置端点标题

SubscriptionWSImplServiceClient client = new SubscriptionWSImplServiceClient();

//Create wsse security object
Security security = new Security();
UsernameToken usernameToken = new UsernameToken { Password = password, Username = uname };
security.UsernameToken = usernameToken;

//Serialize object to xml
XmlObjectSerializer xmlObjectSerializer = new DataContractSerializer(typeof(Security), "Security", "");

//Create address header with security header
AddressHeader addressHeader = AddressHeader.CreateAddressHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", security, xmlObjectSerializer);

//Create new endpoint with addressHeader that contains security header
EndpointAddress endpoint = new EndpointAddress(new Uri("http://127.0.0.1/mpp/subscriptionService"), new[] { addressHeader });

//Set new endpoint
client.Endpoint.Address = endpoint;

0 个答案:

没有答案