亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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有了這個你會得到一個簡單的代碼,它有一個操作契約,它接受一個整數并返回一個字符串?,F在我正在嘗試使用自簽名 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 功能

http://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>

接下來在網站上添加對服務的引用

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

發現服務并添加您創建的服務?,F在構建您的解決方案并發布它。**注意不要將解決方案發布在桌面或文檔等用戶目錄中,否則 ACL 權限會讓您頭疼,而是直接將其發布在 Drive 中的目錄中。

現在托管時間 首先讓我們打開 IIS(始終以管理員身份)并創建一個新證書。

在服務器上轉到 IIS 部分并選擇服務器證書,然后單擊右端的創建新證書。

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

現在從左側菜單創建一個新網站

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

http://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 版本。

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

現在,如果您瀏覽 HTTPS 鏈接,您會收到服務已創建的消息

http://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服務庫項目模板,因為它通常用作類庫。

http://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 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號