c# - 在需要调用证书的 asp.net 中创建 HTTPS 安全 SOAP 网络服务

标签 c# ssl iis soap

我正在尝试创建一个soap Web服务,该服务将具有一个将名称作为输入的Web方法,并将通过HTTPS返回“hello”+name,并且需要添加一些证书才能被调用。

我在 IIS 中创建了一个自签名证书,并添加了带有证书的 HTTPS 绑定(bind)。我已在默认网站下添加了我的应用程序并在其上启用了 SSL。

在 Visual Studio 中,我创建了 WCF 服务应用程序,并在其上启用了 SSL。添加了在类中仅包含一种方法的 asmx 文件。这是代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace POCSoap
{
    [WebService(Namespace = "https://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld(string name)
        {
            return "Hello "+name;
        }
    }
}

我还向 web.config 添加了绑定(bind)和服务声明。
<services>
          <service name="POCSoap.Service1">
              <endpoint address=""
                        binding="basicHttpBinding"
                        bindingConfiguration="secureHttpBinding"
                        contract="POCSoap.IService1"/>

              <endpoint address="mex"
                        binding="mexHttpsBinding"
                        contract="IMetadataExchange" />
          </service>
      </services>
      <bindings>
          <basicHttpBinding>
              <binding name="secureHttpBinding">
                  <security mode="Transport">
                      <transport clientCredentialType="None"/>
                  </security>
              </binding>
          </basicHttpBinding>
      </bindings>

尽管如此,通过 http 调用仍然有效,通过 https 调用无效。这里缺少什么。

最佳答案

WCF 服务是一种不同于 XML Web 服务 (ASMX) 的 Web 服务。它们是不同的 Web 服务规范。
为了使 WCF 服务通过 HTTPS 工作,我们应该像您一样定义一个具有传输安全性的服务端点以及服务契约(Contract)、服务实现。此外,在 HTTPS 上工作需要 IIS 站点绑定(bind)中的 Https 绑定(bind)。
IService.cs

namespace POCSoap
{
    [ServiceContract]
    public interface IService1
    {
        string HelloWorld(string name);
        }}

服务1.cs
public class Service1 : IService1
    {
        public string HelloWorld(string name)
        {
            return "Hello " + name;
        }

但是,调用是由客户端代理完成的,而不是直接发送 HTTP 请求。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client#add-service-reference-in-visual-studio
至于 ASMX 服务,我们也需要使用客户端代理来调用它。因此,我想知道当您已经在 IIS 中添加了 https 绑定(bind)时,为什么通过 https 的调用不起作用。
要在 WCF 中创建一个可以直接通过简单的 Http 请求(http 动词,请求体)调用的 Rest API,我们需要改变创建 WCF 服务的方式。
<system.serviceModel>
    <services>
      <service name="WcfService3.Service1">
        <endpoint address="" binding="webHttpBinding" contract="WcfService3.IService1" behaviorConfiguration="rest"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

最后,你是什么意思要求证书被调用?您想使用证书对客户端进行身份验证,对吗?这取决于您使用的 Web 服务,WCF 服务或 Xml Web 服务。
如果有什么我可以帮忙的,请随时告诉我。

关于c# - 在需要调用证书的 asp.net 中创建 HTTPS 安全 SOAP 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358252/

相关文章:

c# - IDataErrorInfo 调用绑定(bind)对象而不是 DataContext

ssl - 让我们加密与 cloudflare 或两者兼而有之?

c# - 无法远程返回自定义 HTTP 错误详细信息

iis - 无法生成临时类

iis - WebSocket 托管不在端口 80 防火墙问题

c# - 从数据库编码 URL 查询字符串

c# - WPF 窗体的布局 - 水平列表框

c# - 在 geckofx 中显示 html

java - 如何知道 x509 证书是否由 RSA 签名?

php - 无法使用 twilio 发送短信发生错误