【问题标题】:WCF service with both JSON and sessions?带有 JSON 和会话的 WCF 服务?
【发布时间】:2013-03-18 17:29:30
【问题描述】:

是否可以让 WCF 服务使用 JSON 作为请求和响应格式来支持会话?

我需要保护我的应用程序,该应用程序当前正在与 JSON 格式的 WCF 服务器通信并在每次调用时进行身份验证。

我只想在登录时进行身份验证,然后在会话中处理其余的请求,但是当我尝试创建这样的服务时,我需要将绑定更改为 wsHttpBinding 或 netTCPBinding,而那一刻我这样做了,服务器不再接受我的 JSON 请求。但是,它确实接受了来自我用 C# 编写的测试客户端的请求,只需使用“添加服务引用”工具即可。

使用 fiddler,我发现 C# 客户端通过大量臃肿的 XML 与我的服务通信。

这是我的 web.config:

<?xml version="1.0"?>

<configuration>

  <system.web>
    <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral,     PublicKeyToken=b77a5cxxxxxxe089"/>
      </assemblies>
    </compilation>
    <!--
          The <authentication> section enables configuration 
          of the security authentication mode used by 
          ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Windows"/>
    <!--
           The <customErrors> section enables configuration 
           of what to do if/when an unhandled error occurs 
           during the execution of a request. Specifically, 
           it enables developers to configure html error pages 
           to be displayed in place of a error stack trace.

           <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
             <error statusCode="403" redirect="NoAccess.htm" />
             <error statusCode="404" redirect="FileNotFound.htm" />
           </customErrors>
        -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MobiServiceLibrary.MobiServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <!--<endpointBehaviors>
        <behavior name="MobiServiceLibrary.MobiServiceBehavior">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"     automaticFormatSelectionEnabled="true"/>
        </behavior>
      </endpointBehaviors>-->
    </behaviors>
    <services>
      <service behaviorConfiguration="MobiServiceLibrary.MobiServiceBehavior" name="MobiServiceLibrary.MobiService">
        <endpoint address="" binding="wsHttpBinding" contract="MobiServiceLibrary.IMobiService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true"     minFreeMemoryPercentageToActivateService="1">
        </serviceHostingEnvironment>-->
  </system.serviceModel>
</configuration>

我的登录“合同”:

[ServiceContract(SessionMode = SessionMode.Required)]
public partial interface IMobiService
{
    [OperationContract(IsInitiating=true)]
    [WebInvoke(Method = "POST", UriTemplate = "/Login", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
    userData login(string pUserName, string pPassword, string pDeviceType);
}

我的登录“实施”:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,AddressFilterMode=AddressFilterMode.Any)]
public partial class MobiService : IMobiService
{
    public userData login(string pUserName, string pPassword, string pDeviceType)
    {
        //Do Login
    }
}

【问题讨论】:

    标签: c# ios json wcf session


    【解决方案1】:

    您是否将您的 wcf 服务标记为 scriptService?

    此 WCFService 是托管在 IIS 服务器上还是仅托管在服务主机上。我不知道服务主机如何管理身份验证。我知道它适用于 IIS

    从您的应用调用登录服务。如果成功,IIS 将创建一个会话 cookie,您可以从应用程序的响应中获取它。缓存该 cookie 并将其设置在您从应用程序发出的每个后续服务请求中

    【讨论】:

    • 如何将服务标记为 scriptService?通过 WebConfig 还是 Contract 属性?我在 IIS8 上托管,但 II7 也没有工作。我还在本地 PC 上直接从 Visual Studio 运行它以进行调试。当我在本地运行的服务上使用 fiddler 调用登录时,我收到一个错误返回:HTTP/1.1 415 Unsupported Media Type。在 IIS8 上,做同样的事情,我得到这个错误:HTTP/1.1 415 Cannot process the message because the content type 'application/json' is not the expected type 'application/soap+xml; charset=utf-8'.
    • 这意味着您的登录服务不需要 json。它正在寻找肥皂。所以你必须向登录服务发出一个soap请求。
    【解决方案2】:

    我将尝试回答我自己的问题,如果我仍然错了,请随时指出。

    在谷歌搜索之后,我发现了这个:BasicHttpBinding vs WsHttpBinding vs WebHttpBinding

    这解释了为什么我在运行 JSON 格式的会话服务时遇到问题。 似乎绑定是主要问题: webHttpBinding 用于纯 RestFul 服务,与 JSON 配合良好,服务和客户端之间的请求和响应几乎没有包装,建议每次调用重新认证。 basicHttpBindingwsHttpBinding 都用于为服务添加额外的功能和安全性(wsHttpBinding 只是更新并具有更多功能)。 如果我理解正确,basic-binding 和 ws-binding 都依赖 SOAP+XML 以支持附加功能,其中之一是 session-support。 p>

    因此,根据定义,basicHttpBinding 和 wsHttpBinding 服务不是 RESTful 且 webHttpBinding 服务不支持会话。

    所以我的选择是:

    1. 坚持使用 webHttpBinding 并对每次调用进行身份验证(容易受到重放攻击,http://en.wikipedia.org/wiki/Replay_attack
    2. 坚持使用 webHttpBinding 并使用数据库服务器端来实现我自己的“会话”版本。 (最好是通过实施非对称加密,例如 RSA)
    3. 修改客户端以支持 SOAP+XML

    目前我倾向于选择 2。

    我仍然会感谢任何 cmets、建议和/或批评,以及任何可以帮助我更好地理解这一点的东西。

    问候!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2012-10-20
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多