【问题标题】:Solution with MVC/WebApi and WCF - Authentications使用 MVC/WebApi 和 WCF 的解决方案 - 身份验证
【发布时间】:2017-07-03 03:43:27
【问题描述】:

我被分配了一个当前使用 WCF 服务的旧项目。

重点是更新(更像是重新开始)项目以使用 ASP.NET MVC5 和 Web API。问题是第 3 方使用 WCF 服务,他们可能不愿意对通信结束进行更改。

该项目的主要功能有两个基本,一个是接收数据,保存并仅显示几个主题和历史图表的最后状态,另一个是发送数据(打开/关闭主题)。

我的想法是按原样维护 WCF 服务(接收/发送/保存数据),只需将其添加到由 MVC 和 Web API(读取数据)组成的新解决方案中。 他们需要(我认为)在同一个项目中,因为最终目标是尽可能在 WCF 服务上实现 SignalR。

主要问题是 MVC 和 WebAPI 将保留身份验证,但 WCF 不会。当我尝试在同一个项目上测试 WCF 时,它失败了,因为它要求“登录”。

错误:无法从 http://localhost:50598/ServiceTest.svc 如果这是 Windows (R) 请访问您有权访问的通信基础服务 检查您是否已在指定位置启用元数据发布 地址。有关启用元数据发布的帮助,请参阅 MSDN 文档位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata交流 错误 URI:http://localhost:50598/ServiceTest.svc 元数据包含 无法解决的参考: 'http://localhost:50598/ServiceTest.svc'。内容类型 text/html; charset=utf-8 响应消息的内容类型不匹配 绑定(应用程序/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>

我还将 routes.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 项目中,而不受登录障碍的影响。

【问题讨论】:

    标签: c# asp.net asp.net-mvc wcf asp.net-web-api


    【解决方案1】:

    在回答问题 1 时,是的,您的客户必须进行修改。 WCF 是 SOAP/XML,而 WebApi 对于初学者来说是 REST/JSON(通常)。 至于2,如果WCF服务工作正常,就让它保持原样。您不需要直接将它包含在项目中,只需使用 Visual Studio 中的“添加服务引用”向导来制作一个与之交互的客户端。 附带说明一下,您遇到的错误可能是由于使用带有 Visual Studio 的内置 IIS express 实例,它不支持同时运行匿名和经过身份验证的应用程序,因此您的 WCF 服务最终启用了身份验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多