如何在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;
}
所以这就是我到目前为止所拥有的! 使用此命令会返回错误说明:
未提供客户端证书。指定客户端证书 在ClientCredentials中。
有人有想法吗?要温柔,我是新手!
答案 0 :(得分:9)
正如在其他答案的评论中发现的那样,您需要直接设置service.ClientCredentials.ClientCertificate.Certificate
。
答案 1 :(得分:0)
可能需要查看该证书是否对计算机帐户可见 - 假设您已调试到以下某点:
var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0];
并且说它找不到它?
答案 2 :(得分:0)
我认为你缺少合同说明。这里是一个关于如何做的小例子。如果你还有其他问题,我有完整的工作代码。我会帮助你的。
ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService),typeof(EvalServiceClient));
在打开代理之前,您必须使用客户端进行初始化。希望它会奏效。
玩得开心