【问题标题】:How to self-host a WCF REST XML Service over https without using app.config?如何在不使用 app.config 的情况下通过 https 自托管 WCF REST XML 服务?
【发布时间】:2014-05-09 14:24:28
【问题描述】:

使用以下代码,我的服务在 http 上运行良好:

WebServiceHost serviceHost = new WebServiceHost(typeof(RestInterface), new Uri(http://localhost/rest));
serviceHost.Open();

当我尝试通过 https 托管完全相同的服务时,它不起作用。我在服务跟踪查看器中一无所获,Chrome 只是说“此网页不可用”。这是我用于 https 的代码:

var serviceHost = new WebServiceHost(typeof(RestInterface), new Uri(https://localhost/rest));

serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
    X509CertificateValidationMode.Custom;

serviceHost.Credentials.ClientCertificate.Authentication.CustomCertificateValidator =
    new CertValidator(); // Custom validator that will accept any cert

serviceHost.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindBySubjectName,
    "localhost");

var binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.Transport;

var ep = serviceHost.AddServiceEndpoint(typeof(IRestInterface), binding, "");
ep.EndpointBehaviors.Add(new WebHttpBehavior());

serviceHost.Open();

有人搞定这个吗?我做错了什么?!?

【问题讨论】:

    标签: .net web-services wcf rest c#-4.0


    【解决方案1】:

    好吧,我终于想通了。我需要将证书绑定到端口。不要这样做:

    serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
        X509CertificateValidationMode.Custom;
    
    serviceHost.Credentials.ClientCertificate.Authentication.CustomCertificateValidator =
        new CertValidator(); // Custom validator that will accept any cert
    
    serviceHost.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindBySubjectName,
        "localhost");
    

    改为:

    Process bindPortToCertificate = new Process();
    bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");
    bindPortToCertificate.StartInfo.Arguments = string.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}", epAddress.Port, cert.Thumbprint, Guid.NewGuid());
    bindPortToCertificate.Start();
    bindPortToCertificate.WaitForExit();
    

    取自: http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx

    这是我的工作代码。它仍然需要清理一下:

    WebHttpBinding binding = new WebHttpBinding();
    binding.Security.Mode = WebHttpSecurityMode.Transport;
    serviceHost = new ServiceHost(typeof(RestInterface));
    
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadWrite);
    var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", false)[0];
    store.Close();
    
    
    Process bindPortToCertificate = new Process();
    bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");
    bindPortToCertificate.StartInfo.Arguments = string.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}", epAddress.Port, cert.Thumbprint, Guid.NewGuid());
    bindPortToCertificate.Start();
    bindPortToCertificate.WaitForExit();
    
    var ep = serviceHost.AddServiceEndpoint(typeof(IRestInterface), binding, epAddress);
    ep.EndpointBehaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
    

    【讨论】:

    • 很好,这行得通。但是,我仍然收到信任错误 (DLG_FLAGS_INVALID_CA):“您的 PC 不信任此网站的安全证书。”有没有简单的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多