如何使用WCF在WSSE安全标头中设置密码类型“PasswordText”

时间:2015-07-03 07:20:55

标签: c# wcf wsse

我希望有一个wsse安全标头,密码类型设置为“PasswordText”,如下面的SOAP snipet所示:

<wsse:UsernameToken wsu:Id="UsernameToken-2">
   <wsse:UsernaWme>usrnm</wsse:Username>
   <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pwd</wsse:Password>
</wsse:UsernameToken>

但我得到的只是一个没有Type属性的简单密码标签。

<wsse:UsernameToken u:Id="UsernameToken-2">
  <wsse:Username>usrnm</wsse:Username>
  <wsse:Password>pwd</wsse:Password>
</wsse:UsernameToken>

在代码或app.cfg中指定它的正确方法是什么? 到目前为止,Hier是我的代码:

service.ClientCredentials.UserName.UserName = "usrnm";
service.ClientCredentials.UserName.Password = "pwd";

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.AllowInsecureTransport = true;

var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
var transportElement = new HttpTransportBindingElement();

var binding = new CustomBinding(securityElement, encodingElement, transportElement);
service.Endpoint.Binding = binding;

1 个答案:

答案 0 :(得分:0)

也许这会帮助那些像我一样在搜索中发现这一点的人。我通过更改MessageSecurityVersion解决了这个问题。

在app.config自定义绑定中:

<security authenticationMode="UserNameOverTransport" 
   messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" />

在C#中:

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;