简体   繁体   English

将WCF服务客户端实例从app.config重构为代码

[英]Refactoring WCF service client instantiation from app.config to code

I have a WCF service reference which I use with the following client side app.config: 我有一个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>

I now need to refactor the instantiation to work with a blank app.config: 现在,我需要重构实例以与空白的app.config一起使用:

Here is what I've tried: 这是我尝试过的:

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);

This throws an exception: 这将引发异常:

System.ServiceModel.Security.MessageSecurityException: Client cannot determine the Service Principal Name based on the identity in the target address ' http://myservice:8080/ManagerService.svc ' for the purpose of SspiNegotiation/Kerberos. System.ServiceModel.Security.MessageSecurityException:出于SspiNegotiation / Kerberos的目的,客户端无法基于目标地址“ http:// myservice:8080 / ManagerService.svc ”中的身份来确定服务主体名称。 The target address identity must be a UPN identity (like acmedomain\\alice) or SPN identity (like host/bobs-machine). 目标地址身份必须是UPN身份(例如acmedomain \\ alice)或SPN身份(例如主机/鲍勃机)。

I have checked, that the correct certificate is found in the store. 我已经检查过,在商店中找到了正确的证书。 I would be very glad to get a hint on what I am missing here and whether the way I'm going here is ok in general...? 我很高兴得到我在这里缺少什么的提示,以及我通常在这里走的路是否还可以...?

您丢失了行clientCredentialType="UserName"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM