以编程方式使用证书身份验证配置 WCF 服务客户端

问题描述 投票:0回答:3

如何在 C# 中以编程方式使用证书身份验证设置 ServiceClient?
我不想使用.config。

       using(var srv = GetServiceInstance())
       {
            srv.DoStuff()
        }

        private  TheServiceClient GetServiceInstance()
        {
            var service = new TheServiceClient(CreateWsHttpBinding(), CreateEndpointAdress());
            return service;
        }
        private static WSHttpBinding CreateWsHttpBinding()
        {
        var wsHttpBinding = new WSHttpBinding();
        wsHttpBinding.Security.Mode = SecurityMode.Message;

        wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        wsHttpBinding.Security.Transport.Realm = "";
        wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

        wsHttpBinding.Security.Message.NegotiateServiceCredential = true;
        wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;

        wsHttpBinding.Name = "Bindingname";
        wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1);
        wsHttpBinding.BypassProxyOnLocal = false;
        wsHttpBinding.TransactionFlow = false;
        wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        wsHttpBinding.MaxBufferPoolSize = 524288;
        wsHttpBinding.MaxReceivedMessageSize = 65536;
        wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
        wsHttpBinding.TextEncoding = Encoding.UTF8;
        wsHttpBinding.UseDefaultWebProxy = true;
        wsHttpBinding.AllowCookies = false;

        wsHttpBinding.ReaderQuotas.MaxDepth = 32;
        wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384;
        wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192;
        wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096;
        wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384;

        wsHttpBinding.ReliableSession.Ordered = true;
        wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
        wsHttpBinding.ReliableSession.Enabled = false;

        return wsHttpBinding;
    }
        private static EndpointAddress CreateEndpointAdress()
        {
            var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0];
            store.Close();

        var endpointIdentity = EndpointIdentity.CreateX509CertificateIdentity(cert);
        var endpoint = new EndpointAddress(new Uri("ServiceUri"), endpointIdentity);

        return endpoint;
    }

这就是我到目前为止所拥有的! 使用它会返回一个错误:

未提供客户端证书。指定客户端证书 在客户凭证中。

有人有想法吗?温柔一点,我是新手!

c# wcf wcf-security wcf-binding wcf-client
3个回答
10
投票

正如在其他答案的评论中发现的,您需要直接设置

service.ClientCredentials.ClientCertificate.Certificate


0
投票

可能需要查看该证书是否对计算机帐户可见 - 假设您已调试到以下点:

var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0];

它说找不到它?


0
投票

我认为您缺少合同描述。这里有一个关于如何做到这一点的小例子。如果您还有其他问题,我有完整的工作代码。我会帮助你。

ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));

在打开代理之前,您必须先与客户端进行初始化。希望它会起作用。

玩得开心

© www.soinside.com 2019 - 2024. All rights reserved.