为了账号安全,请及时绑定邮箱和手机立即绑定

使用 SSL 加密在 IIS 10 上托管 WCFService 库

使用 SSL 加密在 IIS 10 上托管 WCFService 库

C#
白衣染霜花 2022-11-21 20:07:58
我正在尝试使用自签名 SSL 在 IIS 10 上托管 WCF 服务库。要获得最小的完整可验证示例,请打开 Visual Studio 2017New>Project>C#>Web>WCFLibrary有了这个你会得到一个简单的代码,它有一个操作契约,它接受一个整数并返回一个字符串。现在我正在尝试使用自签名 SSL 在 IIS 上托管它(我有更多的操作合同,但这会做)。到目前为止我已经尝试过什么。现在下一部分是在 IIS 上托管它,所以我创建了 SVC 文件我的 SVC 文件包含 -><%@ ServiceHost Language = "C#" Debug = "true" Service = "WcfServiceLibrary2.Service1" %>然后我能找到的所有教程都编辑 Web.Config,这在 Visual Studio 2017 中不可用,所以我尝试了两件事 1. 创建一个 Web.Config 文件并添加配置 2. 发布网站然后获取 Web.Config which不需要任何改变然后我继续 IIS(作为管理员)并添加了一个新网站然后在尝试浏览时,为了查看托管 IIS 服务的消息,我收到此错误“无法读取配置文件”为了解决这个问题,我遵循了 success.trendmicro.com/…现在那个错误消失了,但现在我得到了为了解决这个问题,我遵循了 IIS - 401.3 - 未经授权但这导致浏览器让我浏览目录而不是给出已创建服务的消息。知道我在这里缺少什么吗?我肯定在这里遗漏了一些重要的东西,因为我未能在 HTTP 本身上托管它,我在网上找到的所有教程都有一个文件 Web.Config 而不是 App.config,我正在寻找一个示例(最好是带有图像)来演示它就用这个小例子。我知道这并不遵循所有关于提问的 SO 指南,但我未能将其阐明为一个问题。编辑根据 LexLi 的建议,它可能已经被托管,我去尝试使用 svcutil 来使用它,这给了我错误WS-Metadata Exchange Error    URI: http://localhost/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost/Service1.svc'.    The remote server returned an unexpected response: (405) Method Not Allowed.    The remote server returned an error: (405) Method Not Allowed.HTTP GET Error    URI: http://localhost/Service1.svc    There was an error downloading 'http://localhost/Service1.svc'.    The request failed with HTTP status 404: Not Found.Url 是正确的,因为我是通过使用 IIS 的浏览功能获得的。
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

First Credit where credit is due, many issues I was pointed in the right direction by multiple SO users to name a few would be: Mikael bolinder , Abraham Qian , Lex Li , C# 聊天室里的好人和一个合作伙伴我的工作人员(在编写此答案时没有 SO 帐户),在这个答案中,我计划涵盖使用 HTTPS 安全性在 IIS 服务器中托管 WCF 库可能需要的所有内容。

我使用的工具:

  • Visual Studio 2017 专业版

  • IIS 10(Windows 10 自带,但必须在 Windows 功能中激活)(见下文)

首先:确保您已安装 visual studio 所需的所有组件。

  1. Windows -> .Net 桌面开发

  2. Windows -> Universal Windows platfor development

  3. Web & Cloud -> ASP.NET 和 Web 开发

在这个列表和其他列表中,可能会包含一些额外的组件,原因是我安装了它们,但无法验证它们是否绝对必要。

现在,让我们添加必要的 Windows 功能。控制面板 -> 程序 -> 打开或关闭 Windows 功能

//img1.sycdn.imooc.com//637b6a5700012f9606530359.jpg

确保进入 WCF 服务并检查 HTTP 激活,不要被方块愚弄(我的错误之一)


现在让我们开始创建服务。打开 Visual Studio File -> New -> Project -> Visual C# -> Web -> WCF -> WCF Service Library这会生成您尝试托管的 MCVE


现在您必须将它与网站链接以生成 Web.Config 文件和 SVC 文件,为此,在 Solutions Explorer 上,右键单击您的解决方案,Add-> New Website。


现在在 web.config 文件中添加


<system.serviceModel>

    <services>

      <service name="WcfServiceLibrary4.Service1"> <!-- Change the library name as per your need -->

        <endpoint address=""

                  binding="wsHttpBinding"

                  bindingConfiguration="secureHttpBinding"

                  contract="WcfServiceLibrary4.IService1"/> <!-- Change the library name as per your need -->


        <endpoint address="mex"

                  binding="mexHttpsBinding"

                  contract="IMetadataExchange" />

      </service>

    </services>

    <bindings>

      <basicHttpBinding>

        <binding name="secureHttpBinding">

          <security mode="Transport">

            <transport clientCredentialType="None"/>

          </security>

        </binding>

      </basicHttpBinding>

    </bindings>

    <behaviors>

      <serviceBehaviors>

        <behavior>

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

          <serviceMetadata httpsGetEnabled="true"/>

          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  </system.serviceModel>

接下来在网站上添加对服务的引用

//img1.sycdn.imooc.com//637b6a640001f8cb03390452.jpg

发现服务并添加您创建的服务。现在构建您的解决方案并发布它。**注意不要将解决方案发布在桌面或文档等用户目录中,否则 ACL 权限会让您头疼,而是直接将其发布在 Drive 中的目录中。

现在托管时间 首先让我们打开 IIS(始终以管理员身份)并创建一个新证书。

在服务器上转到 IIS 部分并选择服务器证书,然后单击右端的创建新证书。

//img1.sycdn.imooc.com//637b6a700001d1b502410194.jpg

现在从左侧菜单创建一个新网站

//img1.sycdn.imooc.com//637b6a790001a32502820170.jpg

//img1.sycdn.imooc.com//637b6a7f00011bdc05800556.jpg

确保切换到 https 并在此处选择您的证书,现在要验证您的服务是否已创建,您需要浏览您创建的网站 svc 文件,遗憾的是此时您会收到一条错误消息,说Server Error in '/' Application. Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https].我无法找到问题的原因错误,但我能够找到绕过它的方法,

绕过此错误的步骤 -> 从左侧菜单中选择您的网站,在右键单击绑定的菜单中添加一个 HTTP 绑定,使用不同的端口。在此之后,您将能够浏览您的 svc 文件的 HTTPS 版本。

//img1.sycdn.imooc.com//637b6a8a0001b4c206510283.jpg

现在,如果您浏览 HTTPS 链接,您会收到服务已创建的消息

//img1.sycdn.imooc.com//637b6a9400017de506560320.jpg

现在您可以继续创建一个使用该服务的应用程序。

什么样的未来

  1. 我将尝试找到一种方法来执行此操作而无需添加额外的绑定

  2. 第二个目标是在不添加额外网站的情况下实现这一目标。

如果我实现了这些,我会更新,但是这些不是我现在的优先事项,可能会在很长一段时间内不会添加,如果有任何不合理的地方,或者您有任何改进的想法,请在下面评论。


查看完整回答
反对 回复 2022-11-21
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

首先请不要将IIS物理路径设置为桌面,这样会造成权限问题。我们可以将IIS站点的物理路径设置为C分区下的文件夹。
第二,请看下面的链接,主要说明WCF服务库项目无法直接发布到IIS,因为其Appconfig无法被IIS识别,除非在网站根目录手动添加额外的Webconfig。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project
一般而言,我们使用包含自动生成的 Webconfig 文件的 WCF 服务项目模板而不是 WCF服务库项目模板,因为它通常用作类库。

//img1.sycdn.imooc.com//637b6aad0001298909330399.jpg

默认情况下,该服务由 BasicHttpBinding 托管,它取决于以下标记。


<protocolMapping>

        <add binding="basicHttpBinding" scheme="http" />

</protocolMapping>

该服务也可以通过以下方式手动配置。这两种方式配置文件是等价的。


<system.serviceModel>

    <services>

      <service name="WcfService1.Service1" behaviorConfiguration="mybehavior">

        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>

      </service>

    </services>

    <bindings>

      <basicHttpBinding>

        <binding name="mybinding">

          <security mode="None">

          </security>

        </binding>

      </basicHttpBinding>

</bindings>

不需要在 webconfig 中分配服务端点地址。应该在IIS站点绑定模块中完成。

WCF System.ServiceModel.EndpointNotFoundException

最后,我们可以通过添加额外的服务端点来通过 https 托管服务,请参考以下配置(简化配置)。


<protocolMapping>

  <add binding="basicHttpBinding" scheme="http"/>

    <add binding="basicHttpsBinding" scheme="https" />

</protocolMapping>

然后在IIS站点绑定模块中添加https协议。

配置 Web.config 以发布 WCF 服务

https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with- ssl

如果有什么我可以帮忙的,请随时告诉我。


查看完整回答
反对 回复 2022-11-21
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

简而言之,这里是步骤:

  1. 创建自签名证书

    1. 添加 SSL 绑定

    2. 为 SSL 配置虚拟目录

    3. 为 HTTP 传输安全配置 WCF 服务

此链接更深入:https ://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl


查看完整回答
反对 回复 2022-11-21
  • 3 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信