2013-03-15 37 views
0

是否有可能使用JSON作为请求和响应格式来支持会话的WCF服务?同时使用JSON和会话的WCF服务?

我需要保护我的应用程序,该应用程序当前正与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 
    } 
} 

回答

0

我将尝试回答我自己的问题,请随时指出我是否仍然存在错误。

做一些更多的谷歌搜索后,我发现这一点:BasicHttpBinding vs WsHttpBinding vs WebHttpBinding

这就解释了为什么我一直有麻烦ruuning一个JSON格式的Sessionful服务。 似乎绑定是主要问题: webHttpBinding用于纯RestFul服务,并与JSON运行良好,很少或根本没有包装服务和客户端之间的请求和响应,并且每次调用建议重新验证。 basicHttpBindingwsHttpBinding都用于为服务添加额外的功能和安全性(wsHttpBinding更简单,功能更多)。 如果我理解正确,基本和ws绑定都依赖于SOAP + XML以支持附加功能,其中一个功能是会话支持

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

所以我的选择是:

  1. 用的WebHttpBinding棒和验证每个呼叫(易受重放攻击,http://en.wikipedia.org/wiki/Replay_attack
  2. 棒用的WebHttpBinding并使用DB服务器端实现自己的“版本sessioning”。 (Prefably通过实施非对称加密,如RSA)
  3. 修改客户端支持SOAP + XML

我倾向于选择2号的时刻。

我仍然会欣赏任何意见,建议和/或批评,任何事情都可以帮助我更好地理解这一点。

问候!

0

不要ü有你的WCF服务标记为scriptService?

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

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

+0

如何将服务标记为scriptService?通过WebConfig或合同属性? 我在IIS8上托管,但II7也没有工作。我也直接从Visual Studio在我的本地PC上运行它进行调试。 当我使用fiddler调用登录时,在本地运行的服务上,返回错误:** HTTP/1.1 415 Unsupported Media Type **。 在IIS8上,完全一样,我得到这个错误:** HTTP/1.1 415由于内容类型'application/json'不是预期类型'application/soap + xml;字符集= UTF-8' **。 – 2013-03-18 07:57:50

+0

我编辑了我的问题以包含代码。 – 2013-03-18 08:15:37

+0

这意味着您的登录服务不期待json。它正在寻找肥皂。所以你必须向登录服务发出肥皂请求。 – Siby 2013-03-25 13:36:37