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

标签 c# wcf wcf-security wcf-binding wcf-client

如何在 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;
    }

这就是我目前所拥有的! 使用它会返回一条错误消息:

The client certificate is not provided. Specify a client certificate in ClientCredentials.

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

最佳答案

正如在对其他答案的评论中发现的,您需要直接设置 service.ClientCredentials.ClientCertificate.Certificate

关于c# - 以编程方式使用证书身份验证配置 WCF 服务客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800743/

相关文章:

c# - MvvmCross - 启用链接所有程序集时缺少 View 模型参数

c# - CheckBoxList 子级 CSS

c# - 使用 WPF 时如何将 Checked 属性状态保存到 Properties.Settings?

c# - WCF - 无法获取元数据

c# - WPF 绑定(bind)命令到 ContextMenu

c# - 客户端如何知道 wcf 服务正在运行?

wcf - 来自 Silverlight 的异步 WCF 调用

java调用应用程序无法与WCF服务通信

.net - 如何将 session 数据加密到 token 中以授权后续 Web 服务调用?

wcf - 在企业架构上的 WCF 服务中实现身份验证的最佳方法是什么?