C#BasicHttpBinding与Silverlight客户端的BasicHttpSecurityMode.Transport

时间:2013-02-24 10:06:08

标签: c# wcf silverlight ssl https


我想用SSL保护Silverlight应用程序。 所以我尝试编写一个概念证明,我在其中托管了两个BasicHttpBindings。一个是BasicHttpSecurityMode.None,另一个是BasicHttpSecurityMode.Transport。

但我无法让第二个运行,VS Tools的WCFTestClient显示此错误消息

// Error: Cannot obtain Metadata from https://localhost:8081/ If this is
// a Windows (R) Communication Foundation service to which you have
// access, please check that you have enabled metadata publishing at the
// specified address.  For help enabling metadata publishing, please
// refer to the MSDN documentation at
// http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
// Error    URI: https://localhost:8081/    Metadata contains a reference
// that cannot be resolved: 'https://localhost:8081/'.    An error
// occurred while making the HTTP request to https://localhost:8081/.
// This could be due to the fact that the server certificate is not
// configured properly with HTTP.SYS in the HTTPS case. This could also
// be caused by a mismatch of the security binding between the client and
// the server.    The underlying connection was closed: An unexpected
// error occurred on a send.    Unable to read data from the transport
// connection: An existing connection was forcibly closed by the remote
// host.    An existing connection was forcibly closed by the remote
// hostHTTP GET Error    URI: https://localhost:8081/    There was an
// error downloading 'https://localhost:8081/'.    The underlying
// connection was closed: An unexpected error occurred on a send.   
// Unable to read data from the transport connection: An existing
// connection was forcibly closed by the remote host.    An existing
// connection was forcibly closed by the remote host

如果有人可以查看我的代码,我会很高兴,我坚持了两天。它需要以编程方式完成。 非常感谢。

几乎整个程序:http://pastebin.com/9j9K43tS

终点

private static readonly Uri UriBase = new Uri("http://localhost:8080/");
private static readonly Uri UriBaseService = new Uri("http://localhost:8080/Basic");

private static readonly Uri UriSecure = new Uri("https://localhost:8081/");
private static readonly Uri UriSecureService = new Uri("https://localhost:8081/Secure");

本作品

private static void BasicHTTPServer()
{
    var binding = new BasicHttpBinding();
    binding.Name = "binding1";
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.Security.Mode = BasicHttpSecurityMode.None;

    // Create a ServiceHost for the CalculatorService type and provide the base address.
    _serviceHost = new ServiceHost(typeof (ServiceBasic), UriBase);

    _serviceHost.AddServiceEndpoint(typeof (IServiceBasic), binding, UriBaseService);
    _serviceHost.AddServiceEndpoint(typeof (IPolicyRetriever), new WebHttpBinding(), "")
                .Behaviors.Add(new WebHttpBehavior());
    var smb = new ServiceMetadataBehavior {HttpGetEnabled = true, HttpGetUrl = UriBase};
    _serviceHost.Description.Behaviors.Add(smb);

    // Open the ServiceHostBase to create listeners and start listening for messages.
    _serviceHost.Open();
    Logger.Log(Server.Basic, string.Format("Open at {0} Service: {1}", UriBase, UriBaseService));
}

这不起作用

private static void SecureHTTPServer()
{
    var binding = new BasicHttpBinding();
    // it doesnt matter if I use BasicHttpsBinding or BasicHttpBinding
    binding.Name = "binding2";
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.Security.Mode = BasicHttpSecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

    // Create a ServiceHost for the CalculatorService type and provide the base address.
    _serviceHostSecure = new ServiceHost(typeof (ServiceBasic), UriSecure);
    _serviceHostSecure.Credentials.ServiceCertificate.Certificate = GetCertificate();
        //load a certificate from file
    _serviceHostSecure.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
        X509CertificateValidationMode.None;

    _serviceHostSecure.AddServiceEndpoint(typeof (IServiceBasic), binding, UriSecureService);
    var webHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
    webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

    _serviceHostSecure.AddServiceEndpoint(typeof (IPolicyRetriever), webHttpBinding, "")
                      .Behaviors.Add(new WebHttpBehavior());
    var smb = new ServiceMetadataBehavior {HttpsGetEnabled = true, HttpsGetUrl = UriSecure};
    _serviceHostSecure.Description.Behaviors.Add(smb);

    // Open the ServiceHostBase to create listeners and start listening for messages.
    _serviceHostSecure.Open();
    Logger.Log(Server.Basic, string.Format("Open at {0} Service: {1}", UriSecure, UriSecureService));
}

0 个答案:

没有答案