Configurer le client de service WCF avec l'authentification de certificat par programme
Comment puis-je configurer un ServiceClient à l'aide de l'authentification par Certificat par programmation en c#?
Et je ne veux pas utiliser .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'est donc ce que j'ai jusqu'à présent!
L'utilisation de ce renvoie un message d'erreur disant :
Le certificat du client n'est pas fourni. Spécifiez un certificat client
dans ClientCredentials.
Quelqu'un a une idée? Être doux, je suis nouveau sur ce!
source d'informationauteur espvar
Vous devez vous connecter pour publier un commentaire.
Comme l'a découvert dans les commentaires sur l'autre réponse, vous devez définir
service.ClientCredentials.ClientCertificate.Certificate
directement.peut-être besoin de voir si ce certificat est visible pour le compte de machine - en supposant que vous avez corrigés pour le point suivant:
var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0];
et il est dit qu'il ne peut pas le trouver?
Je pense que vous êtes absent de la description du Contrat. Ici, c'est un peu exemple sur la façon de le faire. Si vous avez plus de problème, j'ai plein de code de travail. Je vais vous aider.
ContractDescription Contrat = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient));
Vous devez l'initialiser avec le client avant l'ouverture de la procuration. Espérons que cela fonctionnera.
avoir du plaisir