繁体   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