从.net 4 WCF使用ws-security java服务

时间:2014-12-22 18:37:30

标签: winforms wcf .net-4.0 jax-ws

我正在尝试使用Soap标头中的基本用户名/密码令牌来使用JAX-WS Web服务。我已经测试过JAX-WS服务正在使用SoapUI。现在我已经转移到.net客户端了。 .net客户端将是一个Windows窗体应用程序。我添加了服务引用并设置了用户名和密码值。当我执行web方法时,soap xml中没有安全头。根据我在研究中收集的内容,我需要将服务客户端设置为使用消息级安全性。这就是我陷入困境,无法弄清楚的地方。

这是我的代码

MyServiceProxy serverService = new MyServiceProxy.MyServiceProxyClient();
MyServiceProxy inputVO = new MyServiceProxy.getAddressFormatInput();
MyServiceProxy outputVO = new MyServiceProxy.getAddressFormatOutput();
//set username and passwrd
serverService.ClientCredentials.UserName.UserName = "username";
serverService.ClientCredentials.UserName.Password = "PASSWORD";
inputVO.addressNumber = 13602;
inputVO.addressNumberSpecified = true;
outputVO = serverService.GetAddressFormat(inputVO);            

发布此内容

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:orac="http://oracle.e1.bssv.JP550010/">   
<soapenv:Body>
    <orac:GetAddressFormat>         
         <addressNumber>13602</addressNumber>
   </orac:GetAddressFormat>
</soapenv:Body>

当我需要它发布时(注意安全标题)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:orac="http://oracle.e1.bssv.JP550010/">
<soapenv:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soapenv:mustUnderstand=">
    <wsse:UsernameToken>
        <wsse:Username>username</wsse:Username>   
        <wsse:Password>password</wsse:Password>  
    </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>
<soapenv:Body>
    <orac:GetAddressFormat>
        <addressNumber>13602</addressNumber>
    </orac:GetAddressFormat>
</soapenv:Body>
</soapenv:Envelope>

如何让.net将安全信息放入soap标头?

这是我能找到最接近解决方案的地方,但我找不到设置安全模式的方法。

http://weblog.west-wind.com/posts/2012/Nov/24/WCF-WSSecurity-and-WSE-Nonce-Authentication

请帮忙。我提前感谢任何帮助。

这里要求的是app.conf的绑定部分,没有服务部分

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="MyWebServiceProxyPortBinding" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://MyWebServiceProxy"
            binding="basicHttpBinding" bindingConfiguration="MyWebServiceProxyPortBinding"
            contract="MyWebServiceProxyService.MyWebServiceProxy"
            name="MyWebServiceProxyPort" />
    </client>
</system.serviceModel>

使用此链接解决了

With C#, WCF SOAP consumer that uses WSSE plain text authentication?

在代码中我做了以下

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.AllowInsecureTransport = true;
securityElement.EnableUnsecuredResponse = true;
var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11,   Encoding.UTF8);
var transportElement = new HttpTransportBindingElement();            
var binding = new CustomBinding(securityElement, encodingElement, transportElement);

ServiceReference1.MyWebServiceProxyClient client = new     ServiceReference1.MyWebServiceProxyClient();
ServiceReference1.getAddressFormatInput inputVO = new ServiceReference1.getAddressFormatInput();
ServiceReference1.getAddressFormatOutput outputVO = new     ServiceReference1.getAddressFormatOutput();

client.Endpoint.Binding = binding;
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";            
inputVO.addressNumber = 13602;
inputVO.addressNumberSpecified = true;
outputVO = client.GetAddressFormat(inputVO);

3 个答案:

答案 0 :(得分:0)

Security设置在绑定上,通常在XML Config文件中设置。请参阅MSDN

要从代码设置,请使用MyServiceProxyClient(Binding, EndpointAddress)构造函数。

答案 1 :(得分:0)

我相信您需要指定安全设置 - basicHttpBinding的默认设置为None。修改配置文件的<binding>部分,如下所示:

<bindings>
    <basicHttpBinding>
        <binding name="MyWebServiceProxyPortBinding">
          <security mode="Message" clientCredentialType="UserName" />
        </binding>
    </basicHttpBinding>
</bindings>

答案 2 :(得分:0)

使用此链接解决了

With C#, WCF SOAP consumer that uses WSSE plain text authentication?

在代码中我做了以下

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.AllowInsecureTransport = true;
securityElement.EnableUnsecuredResponse = true;
var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11,   Encoding.UTF8);
var transportElement = new HttpTransportBindingElement();            
var binding = new CustomBinding(securityElement, encodingElement, transportElement);

ServiceReference1.MyWebServiceProxyClient client = new     ServiceReference1.MyWebServiceProxyClient();
ServiceReference1.getAddressFormatInput inputVO = new ServiceReference1.getAddressFormatInput();
ServiceReference1.getAddressFormatOutput outputVO = new     ServiceReference1.getAddressFormatOutput();

client.Endpoint.Binding = binding;
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";            
inputVO.addressNumber = 13602;
inputVO.addressNumberSpecified = true;
outputVO = client.GetAddressFormat(inputVO);