簡體   English   中英

MVC / WebApi和WCF解決方案-身份驗證

[英]Solution with MVC/WebApi and WCF - Authentications

我被分配了一個當前使用WCF服務的舊項目。

關鍵是要更新(更像是從頭開始)該項目以使用ASP.NET MVC5和Web API。 問題在於,第三方使用WCF服務,並且他們可能不願意更改其通信的結束。

該項目的主要功能是兩個基本功能,一個是接收數據,保存並僅顯示幾個主題和歷史圖的最新狀態,另一個是發送數據(打開/關閉主題)。

我的想法是按原樣維護WCF服務(接收/發送/保存數據),只需將其添加到包含MVC和Web API(讀取數據)的新解決方案中即可。 他們需要(我認為)在同一個項目中,因為最終目標是在可能的情況下在WCF服務上實現SignalR。

主要問題是MVC和WebAPI將落后於身份驗證,但WCF不會。 當我嘗試在同一項目上測試WCF時,它失敗,因為它要求“登錄”。

錯誤:無法從http:// localhost:50598 / ServiceTest.svc獲取元數據。如果這是您有權訪問的Windows(R)Communication Foundation服務,請檢查是否已在指定地址啟用元數據發布。 有關啟用元數據發布的幫助,請參考MSDN文檔, 網址http://go.microsoft.com/fwlink/?LinkId=65455。WS-MetadataExchange錯誤URI: http:// localhost:50598 / ServiceTest.svc元數據包含無法解析的引用:“ http:// localhost:50598 / ServiceTest.svc ”。 內容類型為text / html; 響應消息的charset = utf-8與綁定的內容類型不匹配(application / soap + xml; charset = utf-8)。 如果使用自定義編碼器,請確保正確實施IsContentTypeSupported方法。 響應的前1024個字節為:'?

登錄。

用戶名http:// localhost:50598 / ServiceTest.svc HTML文檔不包含Web服務發現信息。

我嘗試了所有可以在網上找到的東西。 但是找不到任何可行的方法。

我的最后一個是擺弄web.config:

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="UnsecuredBinding">
          <security mode="None">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="Management.ServiceAspNetAjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="Management.ServiceTest" behaviorConfiguration="serviceBehavior">
        <endpoint address="" behaviorConfiguration="Management.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ServiceLibrary.IService" bindingConfiguration="UnsecuredBinding"/>
      </service>
    </services>
  </system.serviceModel>

我還添加了route.IgnoreRoute到RouteConfig.cs

routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc");

並嘗試將其添加到Global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
   HttpApplication context = (HttpApplication)sender;

   if (context.Request.Url.ToString().Contains(".svc"))
   {
      context.Response.SuppressFormsAuthenticationRedirect = true;
   }
}

我的問題:

  1. 如果將WCF方法遷移到WebAPI,客戶端是否需要在其末端進行任何修改?

  2. 如果是,如何將WCF集成到我的MVC / WebAPI項目中,而不受登錄障礙的影響。

在回答問題1時,是的,您的客戶必須進行修改。 WCF是SOAP / XML,而WebApi通常是初學者的REST / JSON。 至於2,如果WCF服務運行正常,則保持原樣。 您不需要直接將其包含在項目中,只需使用Visual Studio中的“添加服務引用”向導即可使客戶端與之交互。 附帶說明一下,出現錯誤的原因可能是與Visual Studio一起使用了內置的IIS express實例,該實例不支持同時運行匿名和經過身份驗證的應用程序,因此您的WCF服務最終啟用了身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM