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

我正在尝试使用自签名 SSL 在 IIS 10 上托管 WCF 服务库。

要获得最小的完整可验证示例,请打开 Visual Studio 2017

New>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/…

现在那个错误消失了,但现在我得到了

http://img3.mukewang.com/637b6a3c0001ec1113140697.jpg

为了解决这个问题,我遵循了 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 的浏览功能获得的。


白衣染霜花
浏览 96回答 3
3回答

慕标5832272

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:&nbsp;Mikael bolinder&nbsp;,&nbsp;Abraham Qian&nbsp;,&nbsp;Lex Li&nbsp;,&nbsp;C# 聊天室里的好人和一个合作伙伴我的工作人员(在编写此答案时没有 SO 帐户),在这个答案中,我计划涵盖使用 HTTPS 安全性在 IIS 服务器中托管 WCF 库可能需要的所有内容。我使用的工具:Visual Studio 2017 专业版IIS 10(Windows 10 自带,但必须在 Windows 功能中激活)(见下文)首先:确保您已安装 visual studio 所需的所有组件。Windows -> .Net 桌面开发Windows -> Universal Windows platfor developmentWeb & Cloud -> ASP.NET 和 Web 开发在这个列表和其他列表中,可能会包含一些额外的组件,原因是我安装了它们,但无法验证它们是否绝对必要。现在,让我们添加必要的 Windows 功能。控制面板 -> 程序 -> 打开或关闭 Windows 功能确保进入 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>&nbsp; &nbsp; <services>&nbsp; &nbsp; &nbsp; <service name="WcfServiceLibrary4.Service1"> <!-- Change the library name as per your need -->&nbsp; &nbsp; &nbsp; &nbsp; <endpoint address=""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; binding="wsHttpBinding"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bindingConfiguration="secureHttpBinding"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contract="WcfServiceLibrary4.IService1"/> <!-- Change the library name as per your need -->&nbsp; &nbsp; &nbsp; &nbsp; <endpoint address="mex"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; binding="mexHttpsBinding"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contract="IMetadataExchange" />&nbsp; &nbsp; &nbsp; </service>&nbsp; &nbsp; </services>&nbsp; &nbsp; <bindings>&nbsp; &nbsp; &nbsp; <basicHttpBinding>&nbsp; &nbsp; &nbsp; &nbsp; <binding name="secureHttpBinding">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <security mode="Transport">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <transport clientCredentialType="None"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </security>&nbsp; &nbsp; &nbsp; &nbsp; </binding>&nbsp; &nbsp; &nbsp; </basicHttpBinding>&nbsp; &nbsp; </bindings>&nbsp; &nbsp; <behaviors>&nbsp; &nbsp; &nbsp; <serviceBehaviors>&nbsp; &nbsp; &nbsp; &nbsp; <behavior>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <serviceMetadata httpsGetEnabled="true"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- To receive exception details in faults for debugging purposes, set the value below to true.&nbsp; Set to false before deployment to avoid disclosing exception information -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <serviceDebug includeExceptionDetailInFaults="false"/>&nbsp; &nbsp; &nbsp; &nbsp; </behavior>&nbsp; &nbsp; &nbsp; </serviceBehaviors>&nbsp; &nbsp; </behaviors>&nbsp; &nbsp; <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />&nbsp; </system.serviceModel>接下来在网站上添加对服务的引用发现服务并添加您创建的服务。现在构建您的解决方案并发布它。**注意不要将解决方案发布在桌面或文档等用户目录中,否则 ACL 权限会让您头疼,而是直接将其发布在 Drive 中的目录中。现在托管时间&nbsp;首先让我们打开 IIS(始终以管理员身份)并创建一个新证书。在服务器上转到 IIS 部分并选择服务器证书,然后单击右端的创建新证书。现在从左侧菜单创建一个新网站确保切换到 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 版本。现在,如果您浏览 HTTPS 链接,您会收到服务已创建的消息现在您可以继续创建一个使用该服务的应用程序。什么样的未来我将尝试找到一种方法来执行此操作而无需添加额外的绑定第二个目标是在不添加额外网站的情况下实现这一目标。如果我实现了这些,我会更新,但是这些不是我现在的优先事项,可能会在很长一段时间内不会添加,如果有任何不合理的地方,或者您有任何改进的想法,请在下面评论。

慕虎7371278

首先请不要将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服务库项目模板,因为它通常用作类库。默认情况下,该服务由 BasicHttpBinding 托管,它取决于以下标记。<protocolMapping>&nbsp; &nbsp; &nbsp; &nbsp; <add binding="basicHttpBinding" scheme="http" /></protocolMapping>该服务也可以通过以下方式手动配置。这两种方式配置文件是等价的。<system.serviceModel>&nbsp; &nbsp; <services>&nbsp; &nbsp; &nbsp; <service name="WcfService1.Service1" behaviorConfiguration="mybehavior">&nbsp; &nbsp; &nbsp; &nbsp; <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>&nbsp; &nbsp; &nbsp; &nbsp; <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>&nbsp; &nbsp; &nbsp; </service>&nbsp; &nbsp; </services>&nbsp; &nbsp; <bindings>&nbsp; &nbsp; &nbsp; <basicHttpBinding>&nbsp; &nbsp; &nbsp; &nbsp; <binding name="mybinding">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <security mode="None">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </security>&nbsp; &nbsp; &nbsp; &nbsp; </binding>&nbsp; &nbsp; &nbsp; </basicHttpBinding></bindings>不需要在 webconfig 中分配服务端点地址。应该在IIS站点绑定模块中完成。WCF System.ServiceModel.EndpointNotFoundException最后,我们可以通过添加额外的服务端点来通过 https 托管服务,请参考以下配置(简化配置)。<protocolMapping>&nbsp; <add binding="basicHttpBinding" scheme="http"/>&nbsp; &nbsp; <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如果有什么我可以帮忙的,请随时告诉我。

慕婉清6462132

简而言之,这里是步骤:创建自签名证书添加 SSL 绑定为 SSL 配置虚拟目录为 HTTP 传输安全配置 WCF 服务此链接更深入:https ://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl
打开App,查看更多内容
随时随地看视频慕课网APP