【问题标题】:Creating HTTPS secure soap web service in asp.net that requires certificate to be invoked在需要调用证书的 asp.net 中创建 HTTPS 安全肥皂 Web 服务
【发布时间】:2020-10-03 02:18:13
【问题描述】:

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

我在 IIS 中创建了一个自签名证书,并添加了与证书的 HTTPS 绑定。我已经在默认网站下添加了我的应用程序并在其上启用了 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 中添加了绑定和服务声明。

<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 调用是无效的。这里缺少什么。

【问题讨论】:

  • 定义“不工作”。添加您收到的任何错误消息。
  • @Gusman 错误:读取 ECONNRESET

标签: c# ssl iis soap


【解决方案1】:

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

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

Service1.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 绑定时,为什么通过 https 的调用不起作用。
要在WCF中创建一个Rest API,可以直接用一个简单的Http请求(http动词,请求体)调用,我们需要改变创建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 服务。
如果有什么可以帮助的,请随时告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多