WCF, HTTPS vs HTTP

Il y a deux échantillons

Pour HTTP:

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
string addressHttps = String.Format("http://{0}:51222", Dns.GetHostEntry("").HostName);
var wsHttpBinding = new BasicHttpBinding();
var serviceHost = new ServiceHost(typeof (HelloWorldService), new Uri(addressHttps));
Type endpoint = typeof (IHelloWorldService);
serviceHost.AddServiceEndpoint(endpoint, wsHttpBinding, "hello");
Uri uri = new Uri(serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri + "/mex");
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = uri;
serviceHost.Description.Behaviors.Add(smb);
Console.Out.WriteLine("Mex address  " + smb.HttpGetUrl);
try
{
serviceHost.Open();
string address = serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening @ {0}", address);
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
Console.WriteLine();
}
catch (Exception exc)
{
Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
#region IHelloWorldService Members
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
#endregion
}
}

Pour HTTPS

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
string addressHttps = String.Format("https://{0}:51222", Dns.GetHostEntry("").HostName);
var wsHttpBinding = new BasicHttpBinding();
wsHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
var serviceHost = new ServiceHost(typeof (HelloWorldService), new Uri(addressHttps));
Type endpoint = typeof (IHelloWorldService);
serviceHost.AddServiceEndpoint(endpoint, wsHttpBinding, "hello");
serviceHost.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName, "nameofsertificate");
serviceHost.Credentials.ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
Uri uri = new Uri(serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri + "/mex");
var smb = new ServiceMetadataBehavior();
smb.HttpsGetEnabled = true;
smb.HttpsGetUrl = uri;
serviceHost.Description.Behaviors.Add(smb);
Console.Out.WriteLine("Mex address  " + smb.HttpsGetUrl);
try
{
serviceHost.Open();
string address = serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening @ {0}", address);
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
Console.WriteLine();
}
catch (Exception exc)
{
Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
Console.ReadLine();
}
}
public static bool ValidateCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
foreach (X509ChainStatus chainStatus in chain.ChainStatus)
{
if (chainStatus.Status == X509ChainStatusFlags.Revoked)
{
return true;
}
}
}
return false;
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
#region IHelloWorldService Members
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
#endregion
}
}

Ces échantillons sont départ sans erreurs, mais si j'essaie de créer des clients j'ai deux situations différentes:

HTTP - le client a été créé succès en utilisant l'adresse

http://localhost:51222/hello/mex

Et HTTPS échoué. L'adresse HTTPS:

https://localhost:51222/hello/mex

Le message d'erreur pour le HTTPS:

Il y a une erreur de téléchargement
https://localhost:51222/hello/mex.
La connexion sous-jacente a été fermée:
Une erreur inattendue s'est produite sur une
envoyer. L'authentification a échoué car
le distant a fermé la
flux de transport. Les métadonnées contiennent un
de référence qui ne peut pas être résolu:
https://localhost:51222/hello/mex.
Une erreur s'est produite tout en faisant de la
Requête HTTP
https://localhost:51222/hello/mex.
Cela pourrait être dû au fait que la
certificat du serveur n'est pas configuré
correctement avec HTTP.SYS dans le HTTPS
cas. Cela pourrait aussi être causée par une
l'inadéquation de la sécurité de la liaison
entre le client et le serveur. L'
la connexion sous-jacente a été fermée: Une
erreur inattendue s'est produite lors de l'envoi.
L'authentification a échoué car l'
distant a fermé le transport
flux de données. Si le service est défini dans
la solution actuelle, essayez de créer la
solution et l'adjonction d'un service
référence à nouveau.

Où ai-je fais une erreur?

Avez-vous configurer un serveur de certificat pour le https?
Oui, le httpcfg requête ssl retourne résultat suivant IP : 0.0.0.0:51222 Hash : c93258ff 776a9e43ef12f3f90b910521acd4989 Guid : {00000000-0000-0000-0000-000000000000} CertStoreName : MON CertCheckMode : 0 RevocationFreshnessTime : 0 UrlRetrievalTimeout : 0 SslCtlIdentifier : (null) SslCtlStoreName : LOCAL_MACHINE Drapeaux : 0

OriginalL'auteur jitm | 2010-06-29