2015-03-31 73 views
1

我有我下面的客戶端的app.config使用WCF服務引用:重構WCF服務的客戶實例從app.config中代碼

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="WSHttpBinding_ManagerService"> 
       <security> 
        <message clientCredentialType="UserName" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://myadress:8080/ManagerService.svc" 
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ManagerService" 
      contract="Service.ManagerService" name="WSHttpBinding_ManagerService"> 
      <identity> 
       <certificate encodedValue="AwAAAAEAAAAUAAAAaJ8gJ+2BbDC//Nw76gp1Rx4Ii1AgAAAAAQAAACQDAAAwggMgMIICDKADAgECAhB4jlXUsVcUkE45Bq9sj6cdMAkGBSsOAwIdBQAwIzEhMB8GA1UEAxMYc2tpbGxjb25vbXkuY2xvdWRhcHAubmV0MB4XDTA2MTIzMTIyMDAwMFoXDTE5MTIzMTIyMDAwMFowIzEhMB8GA1UEAxMYc2tpbkjsahdjhdjsdhsjhdsjhdjhdsjhdFAAOCAQ8AMIIBCgKCAQEAwHjDPi/A7+4PfvYt40eySE2I6FgVO2Ewco8gJO21TUqHpKbinmsaNTO6wFJy+l3adMRB0dcmAvH938BPgdwbqbVaaG4mRCDpnekEserWmz5ii+ET1xhm0atIg6xW3sgnDOA+41Y0vB8m8AXTfHQYunILQjn/6xGM/RffK32vbR9WGJKEd/okOJ2/vV5dm2UsejlANwK2kCMe9wNRbjaKsH6PIqv26KeHAXxa0tSzoHfrn/lr46+54WzEXFHRzub1JbZk+IsdsdlakasjdksjdjdksjddjskdZZ0Oj0iG0GjvEVbmHWpBM/WhHrqIfGdqiMtXjOtwIDAQAsdsdsdsdsdsdsdaZtgIq+y6hm91EfPUToJ1ZUhWR8z/RG+IVZrs0O93FCMk6WU8OhYxubIgcVSTx0FDCakyOmfu1gnYeEZv53kVPZSmY4KUAUZz+MCQf/OXN2OGv9cRmsWg4iDlHjzDQwucO+rWkclvQo=" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

我現在需要重構實例化一個工作空白的app.config:

這裏是我試過:

var binding = new WSHttpBinding(); 
binding.Security = new WSHttpSecurity { Mode = SecurityMode.Message }; 
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;    
var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser); 
store.Open(OpenFlags.ReadOnly); 
var cers = store.Certificates.Find(X509FindType.FindByThumbprint, "123THUMBPRINTOFCERT", false); 
var cer = cers[0]; 
var identity = new X509CertificateEndpointIdentity(cer); 
var endpoint = new EndpointAddress(new Uri("http://myservice:8080/ManagerService.svc"), identity); 
var client = new ManagerServiceClient(binding, endpoint); 
client.ClientCredentials.UserName.UserName = EMail; 
client.ClientCredentials.UserName.Password = Password; 
var resultBuilder = new StringBuilder(); 
var categories = client.Categorize(Text); 

這將引發異常:

System.ServiceModel.Security.MessageSecurityException:客戶端無法 確定基於在 SspiNegotiation/Kerberos的目的的 目標地址「http://myservice:8080/ManagerService.svc」身份服務主體名稱。目標地址標識 必須是UPN標識(如acmedomain \ alice)或SPN標識(如 主機/ bobs-machine)。

我檢查了在商店中找到了正確的證書。我會很高興得知我在這裏失蹤的內容,以及我在這裏的方式是否一般...?

回答

2

你失去了clientCredentialType="UserName"

binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
+0

你好,非常感謝你!我已經添加了該行(我也更新了該問題),不幸的是我仍然得到相同的異常。你看到其他東西在這裏失蹤了嗎? – Marc 2015-03-31 20:22:05

+0

你明白了......我失蹤了:binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;感謝您的幫助,如果沒有您的提示,本來就很難找到!我已經編輯了你的答案並希望,你對此很酷;) – Marc 2015-04-01 07:00:58