2011-09-23 57 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),並檢查從服務返回的內容。這是錯誤或沒有時間戳的消息。 –

相關問題