通过HTTPS的自托管WebHttpBinding服务

时间:2019-02-12 08:00:18

标签: c# .net rest wcf

我有以下Rest Windows服务,可以通过HTTP完美运行

Uri[] httpBaseAddress = new Uri[] { new Uri("http://localhost:8464/SupportRemote") };

serviceHost = new ServiceHost(typeof(Service.SupportRemoteService), httpBaseAddress);
var Binding = new WebHttpBinding();
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(Model.ISupportRemoteService), Binding, "Rest");
endpoint.Behaviors.Add(new WebHttpBehavior());

foreach (ServiceEndpoint EP in serviceHost.Description.Endpoints)
   EP.Behaviors.Add(new BehaviorAttribute());

var serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(serviceBehavior);
serviceHost.Open();

现在,我想通过具有自签名证书的HTTPS来移动它,但是该服务不起作用。我不知道问题出在哪里

Uri[] httpBaseAddress = new Uri[] { new Uri("https://localhost:8464/SupportRemote") };

serviceHost = new ServiceHost(typeof(Service.SupportRemoteService), httpBaseAddress);
var Binding = new WebHttpBinding(WebHttpSecurityMode.Transport);
Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(Model.ISupportRemoteService), Binding, "Rest");
endpoint.Behaviors.Add(new WebHttpBehavior());

foreach (ServiceEndpoint EP in serviceHost.Description.Endpoints)
   EP.Behaviors.Add(new BehaviorAttribute());

var serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = false;
serviceBehavior.HttpsGetEnabled = true;
serviceHost.Description.Behaviors.Add(serviceBehavior);
serviceHost.Credentials.ServiceCertificate.SetCertificate("CN=MyCertificate", System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.Root);
serviceHost.Open();

1 个答案:

答案 0 :(得分:0)

似乎您已经在服务器端设置了服务器证书,但必须注意一件事。默认情况下,该应用程序没有特权将证书绑定到端口。我们可能会手动将证书绑定到端口。

   {
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "name": "[concat(variables('varnodeNamePrefix'),copyindex(1),'/cse')]",
    "apiVersion": "2017-03-30",
    "location": "[variables('varlocation')]",
    "dependsOn": [
      "[concat(variables('varnodeNamePrefix'),copyindex(1))]"
    ],
    "properties": {
      "publisher": "Microsoft.Compute",
      "type": "CustomScriptExtension",
      "typeHandlerVersion": "1.8",
      "autoUpgradeMinorVersion": true,
      "settings": {
        "fileUris": [
          "https://XXXXXXXXXXX.blob.core.windows.net/powershelscripts/sqlcluster/InstallAdditionalModules.ps1"
        ]
      },
      "protectedSettings": {
        "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted ./sqlcluster/InstallAdditionalModules.ps1",
        "storageAccountName": "sdfsdfsdfsdf",
        "storageAccountKey": "sdsdfsdf/BH9C+fdgdfgdfgdfg+fgdfgdfg=="
      }
    },
    "copy": {
      "name": "WinFeatures",
      "count":"[variables('varvmCount')]"
    }
 },

 {
  "apiVersion": "2015-06-15",
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "name": "[concat(variables('varnodeNamePrefix'),copyindex(1),'/joindomain')]",
  "location": "[resourceGroup().location]",
  "dependsOn": ["[concat(variables('varnodeNamePrefix'),copyindex(1),'/cse')]"            
               ],
  "properties": {
    "publisher": "Microsoft.Compute",
    "type": "JsonADDomainExtension",
    "typeHandlerVersion": "1.3",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "Name": "[variables('vardomainToJoin')]",
      "User": "[concat(variables('vardomainToJoin'), '\\', variables('vardomainUsername'))]",
      "Restart": "true",
      "Options": "[variables('vardomainJoinOptions')]"
    },
    "protectedSettings": {
      "Password": "[variables('vardomainPassword')]"
    }
  },
  "copy": {
    "name": "joindomain",
    "count":"[variables('varvmCount')]"
  }

这是正式文件,希望对您有用。
https://docs.microsoft.com/en-us/windows/desktop/http/add-sslcert
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
此外,如果启用应用程序占用端口时出现问题,最好使用localSystem帐户托管Windows服务。
随时让我知道是否有什么可以帮助您的。