0

是否可以让 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
    }
}
4

2 回答 2

0

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

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

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

于 2013-03-15T13:55:45.733 回答
0

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

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

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

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

所以我的选择是:

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

我目前倾向于选择 2。

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

问候!

于 2013-03-18T09:29:30.793 回答