如何将用户名和密码传递给C#的Web服务调用的SOAP头?

时间:2014-11-20 13:04:35

标签: c# web-services soap soap-client

我想写一个C#代码,它在另一台机器上调用(远程)Web服务。为此,我必须在调用的SOAP标头中传递用户名和密码。

我想知道在C#中实现此代码的示例。

生成的XML应该是:

<env:Header>
    <ns1:Security>
        <ns1:UsernameToken>
            <ns1:Username>XXXXXXXXXXXXXXXX</ns1:Username>
            <ns1:Password>YYYYYYYYYYYYYYYY</ns1:Password>
        </ns1:UsernameToken>
    </ns1:Security>
</env:Header>

提前致谢 学家

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。 CustomBinding更灵活,因为它允许更多的控制,因为我建议你。将标头设置为端点是一种简单的方法:

// binding
var binding = new CustomBinding();
binding.Elements.Clear();
binding.Elements.Add(new TextMessageEncodingBindingElement{MessageVersion = MessageVersion.Soap12});
binding.Elements.Add(new HttpTransportBindingElement{MaxReceivedMessageSize = 20000000,});
// endpoint
var endpoint = new EndpointAddress(new Uri(listeningUri), new MySecurityHeader())
var client = new Client(binding, endpoint);
client.SomeMethod();

其中MySecurityHeader是AddressHeader,例如:

    public class MySecurityHeader : AddressHeader
    {
        public override string Name
        {
            get { return "Security"; }
        }

        public override string Namespace
        {
            get { return "<provide the appropiate namespace>"; }
        }

        protected override void OnWriteAddressHeaderContents(System.Xml.XmlDictionaryWriter writer)
        {
            // here you do what you want
            writer.WriteRaw(String.Format(@"
             <UsernameToken xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
             <Username>user</Username>
             <Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"">pass</Password>
             </UsernameToken>").Trim());
        }
    }

答案 1 :(得分:0)

这是使用IEndpointAdress

的示例
    var customBinding = new CustomBinding();
    customBinding.Elements.Add(new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.Soap12, });
    customBinding.Elements.Add(new HttpTransportBindingElement { MaxReceivedMessageSize = 20000000, });

    var endpointAddres = new EndpointAddress(listeningUri);

    var client = new Client(customBinding, endpointAddres);
    // add my own IEndpointBehavior
    client.ChannelFactory.Endpoint.Behaviors.Add(new CustomBehavior());
    client.SomeMethod();

这是CustomBehavior定义

public class CustomBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {}

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        var inspector = new CustomMessageInspector();
        clientRuntime.MessageInspectors.Add(inspector);
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {}

    public void Validate(ServiceEndpoint endpoint)
    {}
}

public class CustomMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {}

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        request.Headers.Add(new MyMessageHeader());
        return null;
    }
}

public class MyMessageHeader : MessageHeader
{

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        writer.WriteRaw(String.Format(@"
        <UsernameToken xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
        <Username>user</Username>
        <Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"">pass</Password>
        </UsernameToken>").Trim());
    }

    public override string Name
    {
        get { return "MyHeaderName"; }
    }

    public override string Namespace
    {
        get { return "MyHeaderNamespace"; }
    }
}

请注意,您在发送请求之前以及收到回复后都有控制权 我希望这能解决你的问题,如果你遇到一些问题,请问我。