c# - 使用 MVC/WebApi 和 WCF 的解决方案 - 身份验证

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

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

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

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

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

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

Error: Cannot obtain Metadata from http://localhost:50598/ServiceTest.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:50598/ServiceTest.svc Metadata contains a reference that cannot be resolved: 'http://localhost:50598/ServiceTest.svc'. The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: '?

Log in.

Usernamehttp://localhost:50598/ServiceTest.svc The HTML document does not contain Web service discovery information.

我尝试了所有我能在网上找到的东西。但找不到任何有用的东西。

我的最后一个任务是摆弄 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>

我还在 RouteConfig.cs 中添加了 routes.IgnoreRoute

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 中的“添加服务引用”向导制作一个与之交互的客户端即可。 作为旁注,您遇到的错误可能是由于将内置 IIS express 实例与 Visual Studio 一起使用,它不支持同时运行匿名和经过身份验证的应用程序,因此您的 WCF 服务最终启用了身份验证。

关于c# - 使用 MVC/WebApi 和 WCF 的解决方案 - 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233137/

相关文章:

c# - 如何让 WPF TextBlock 在异步设置 Text 属性的位置滚动?

c# - 需要连接 ID

c# - 如何将模型集合从asp.net mvc注入(inject)backbone.js?

c# - 使用组合框显示工具提示(下拉菜单)

c# - 正确使用存储库/服务类

c# - 您如何手动将复杂对象数据绑定(bind)到模板化控件,例如 gridview 中的一行?

javascript - parseFloat 为 -2.00 返回 0-2.00

asp.net-mvc - IIS 7.5 提示输入用户名和密码

c# - 为什么 HTML.TextArea 之前需要一个@?

c# - 可见性变化动画 : property was already registered by 'FrameworkElement'