简体   繁体   English

MVC / WebApi和WCF解决方案-身份验证

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

I have been assigned a old project that currently uses a WCF service. 我被分配了一个当前使用WCF服务的旧项目。

The point is to update (more like starting fresh) the project to use ASP.NET MVC5 and Web API. 关键是要更新(更像是从头开始)该项目以使用ASP.NET MVC5和Web API。 The problem is that a 3rd party uses the WCF service, and they are probably not willing to make changes to their end of the communication. 问题在于,第三方使用WCF服务,并且他们可能不愿意更改其通信的结束。

The main function of the project is two basic, one is receive data, save it and just show the last status of several subjects and history graphs, and the other is to send data (turn on/off subjects). 该项目的主要功能是两个基本功能,一个是接收数据,保存并仅显示几个主题和历史图的最新状态,另一个是发送数据(打开/关闭主题)。

My idea was to maintain the WCF Service (receive/send/save data) as is, just add it to new solution which consists of MVC and Web API (read data). 我的想法是按原样维护WCF服务(接收/发送/保存数据),只需将其添加到包含MVC和Web API(读取数据)的新解决方案中即可。 They need (I think) to be in the same project, because the final objective is to implement SignalR on the WCF Service, if possible. 他们需要(我认为)在同一个项目中,因为最终目标是在可能的情况下在WCF服务上实现SignalR。

The main problem, is that the MVC and WebAPI are going to stay behind Authentication, but the WCF won't. 主要问题是MVC和WebAPI将落后于身份验证,但WCF不会。 At the moment when I try to test the WCF on the same project, it fails because it asks for a "Log in." 当我尝试在同一项目上测试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. 错误:无法从http:// localhost:50598 / ServiceTest.svc获取元数据。如果这是您有权访问的Windows(R)Communication Foundation服务,请检查是否已在指定地址启用元数据发布。 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 '. 有关启用元数据发布的帮助,请参考MSDN文档, 网址http://go.microsoft.com/fwlink/?LinkId=65455。WS-MetadataExchange错误URI: http:// localhost:50598 / ServiceTest.svc元数据包含无法解析的引用:“ http:// localhost:50598 / ServiceTest.svc ”。 The content type text/html; 内容类型为text / html; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). 响应消息的charset = utf-8与绑定的内容类型不匹配(application / soap + xml; charset = utf-8)。 If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. 如果使用自定义编码器,请确保正确实施IsContentTypeSupported方法。 The first 1024 bytes of the response were: '? 响应的前1024个字节为:'?

Log in. 登录。

Usernamehttp://localhost:50598/ServiceTest.svc The HTML document does not contain Web service discovery information. 用户名http:// localhost:50598 / ServiceTest.svc HTML文档不包含Web服务发现信息。

I tried everything that I could find on the web. 我尝试了所有可以在网上找到的东西。 But couldn't find anything that would work. 但是找不到任何可行的方法。

My last consisted on fiddling with the web.config: 我的最后一个是摆弄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>

I also added routes.IgnoreRoute to RouteConfig.cs 我还添加了route.IgnoreRoute到RouteConfig.cs

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

and tried to add this to Global.asax 并尝试将其添加到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;
   }
}

My questions: 我的问题:

  1. If I migrate the WCF methods to WebAPI, will the client need to do any modifications on their end? 如果将WCF方法迁移到WebAPI,客户端是否需要在其末端进行任何修改?

  2. If yes, how can I integrate WCF on my MVC/WebAPI project, without being affected by Log In barrier. 如果是,如何将WCF集成到我的MVC / WebAPI项目中,而不受登录障碍的影响。

In answer to question 1, yes your clients would have to make modifications. 在回答问题1时,是的,您的客户必须进行修改。 WCF is SOAP/XML where as WebApi is REST/JSON(usually) for starters. WCF是SOAP / XML,而WebApi通常是初学者的REST / JSON。 As for 2, if the WCF service is working fine, just leave it as it is. 至于2,如果WCF服务运行正常,则保持原样。 You don't need to include it in the project directly, simply use the "Add Service Reference" wizard in Visual Studio to make a client for interacting with it. 您不需要直接将其包含在项目中,只需使用Visual Studio中的“添加服务引用”向导即可使客户端与之交互。 As a side note, the error you where experiancing was probably due to using the inbuilt IIS express instance with Visual Studio, which doesn't support running anonymous and authenticated applications at the same time, so your WCF service ended up with authentication enabled. 附带说明一下,出现错误的原因可能是与Visual Studio一起使用了内置的IIS express实例,该实例不支持同时运行匿名和经过身份验证的应用程序,因此您的WCF服务最终启用了身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM