2011-09-23 55 views
4

我有这个web.config文件来配置来自外部服务的客户端代理此服务需要在邮件头上进行身份验证,我的服务在我的web.config上配置良好,但现在我尝试使用此代码创建代理,并建议动态更改用户名和密码。以编程方式添加端点头安全性WCF

<cliente> 
    <endpoint address="https://wwwww.hhhahdhadhs.askadadasda" binding="basicHttpBinding" bindingConfiguration="MyConfiguration" contract="PROXY_ACSS_SERVICES.RegistoPrescricaoMedicamentos"> 
      <headers> 
       <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
       <wsse:UsernameToken> 
        <wsse:Username>MyUser</wsse:Username> 
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">87726655434</wsse:Password> 
       </wsse:UsernameToken> 
       </wsse:Security> 
      </headers> 
      </endpoint> 
</cliente> 

<basicHttpBinding> 
<binding name="PrescricaoMedicamentos" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="Transport"> 
      <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> 
      <message clientCredentialType="Certificate" algorithmSuite="Default" /> 
      </security> 
     </binding> 
</basicHttpBinding> 

我的问题是创造一个定义的用户名和密码的头,

<headers> 


    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
      <wsse:UsernameToken> 
       <wsse:Username>MyUser</wsse:Username> 
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">87726655434</wsse:Password> 
      </wsse:UsernameToken> 
      </wsse:Security> 
     </headers> 

在此先感谢

编辑

拉吉斯拉夫Mrnka后,我创建代理程序收到此消息

'请求通道在 00:00:59.9829990后超时尝试发送。将传递给调用的超时值增加到 请求或增加绑定上的SendTimeout值。分配给此操作的时间 可能是更长的 超时的一部分。

这是我的代理配置

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential); 
      binding.Name = "PrescricaoMedicamentos"; 
      binding.CloseTimeout = new TimeSpan(0, 1, 0); 
      binding.OpenTimeout = new TimeSpan(0, 1, 0); 
      binding.ReceiveTimeout = new TimeSpan(0, 10, 0); 
      binding.SendTimeout = new TimeSpan(0, 1, 0); 
      binding.AllowCookies = false; 
      binding.BypassProxyOnLocal = false; 
      binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
      binding.MaxBufferSize = 65536; 
      binding.MaxBufferPoolSize = 524288; 
      binding.MaxReceivedMessageSize = 65536; 
      binding.MessageEncoding = WSMessageEncoding.Text; 
      binding.TextEncoding = System.Text.Encoding.UTF8; 
      binding.TransferMode = TransferMode.Buffered; 
      binding.UseDefaultWebProxy = true; 
      binding.ReaderQuotas = new XmlDictionaryReaderQuotas() 
             { 
              MaxDepth = 32, 
              MaxStringContentLength = 8192, 
              MaxArrayLength = 16384, 
              MaxBytesPerRead = 4096, 
              MaxNameTableCharCount = 16384 
             }; 

      binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; 


      return binding; 

}

我想,如果有可能的静态头中的代码添加到所有消息..喜欢的东西AddressHeader.CreateAddressHeader(“认证头”)并在我的EndpointAddress配置中应用此标题。这种方法替代的文字我的第一个代码

<headers> 
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
      <wsse:UsernameToken> 
       <wsse:Username>myusername</wsse:Username> 
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">mypass</wsse:Password> 
      </wsse:UsernameToken> 
      </wsse:Security> 
     </headers> 

回答

0

如果该服务正确实现用户名令牌轮廓改变你结合的安全设置:

<security mode="TransportWithMessageCredential"> 
    <message clientCredentialType="UserName" /> 
</security> 

而且一旦你创建一个代理凭证集:

proxy.ClientCredentials.UserName.UserName = ...; 
proxy.ClientCredentials.UserName.Password = ...; 
+0

我尝试使用您的建议,但尝试连接服务时,我的客户端应用程序得到此异常'安全处理器无法找到邮件中的安全标头。这可能是因为该消息是不安全的错误,或者是因为通信方之间存在绑定不匹配。如果服务配置了安全性并且客户端没有使用安全性,则会发生这种情况。'你能帮助我吗?谢谢 – mastervv

+0

打开[消息记录](http://msdn.microsoft.com/zh-cn/library/ms730064.aspx),并检查从服务返回的内容。这是错误或没有时间戳的消息。 –

相关问题