WCF, HTTPS vs HTTP

Gibt es zwei Proben

Für 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
    }
}

Für 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
    }
}

Diese Proben sind ab ohne Fehler, aber wenn ich versuche zu erstellen, die clients habe ich zwei verschiedene Situationen:

HTTP - client erstellt wurde erfolgreich mit der Adresse

http://localhost:51222/hello/mex

- Und HTTPS-gescheitert. Die Adresse HTTPS:

https://localhost:51222/hello/mex

Die Fehlermeldung HTTPS:

Es wurde ein Fehler beim downloaden
https://localhost:51222/hello/mex.
Die zugrunde liegende Verbindung wurde geschlossen:
Ein unerwarteter Fehler ist aufgetreten, der auf eine
senden. Die Authentifizierung ist fehlgeschlagen, da
die remote-Partei geschlossen hat, die
transport stream. Metadaten enthält
Verweis, der nicht aufgelöst werden kann:
https://localhost:51222/hello/mex.
Ein Fehler ist aufgetreten, während der
HTTP-Anforderung an
https://localhost:51222/hello/mex.
Dies könnte aufgrund der Tatsache, dass die
server-Zertifikat ist nicht konfiguriert
richtig mit HTTP.SYS in den HTTPS
Fall. Dies könnte auch verursacht werden, durch ein
Missverhältnis des Sicherheits-Bindung
zwischen dem client und dem server. Die
zugrunde liegende Verbindung wurde geschlossen: Eine
unerwarteter Fehler bei einer senden.
Die Authentifizierung ist fehlgeschlagen, da die
remote-Partei geschlossen hat, die transport -
stream. Wenn der Dienst definiert ist, in
die aktuelle Lösung, erstellen Sie die
Lösung und hinzufügen des Dienstes
Referenz wieder.

Wo habe ich einen Fehler gemacht?

  • Haben Sie die Konfiguration ein server Zertifikat für https?
  • Ja, die httpcfg query ssl gibt nächste Folge IP : 0.0.0.0:51222 Hash : c93258ff 776a9e43ef12f3f90b910521acd4989-Guid : {00000000-0000-0000-0000-000000000000} CertStoreName : MEINE CertCheckMode : 0 RevocationFreshnessTime : 0 UrlRetrievalTimeout : 0 SslCtlIdentifier : (null) SslCtlStoreName : LOCAL_MACHINE Flags : 0
InformationsquelleAutor jitm | 2010-06-29
Schreibe einen Kommentar