前提:
VS2010开发ASP.NET项目时,ASP.NET项目与服务端项目(其他项目)进行通信(WCF),基本通信过程包括登录和数据显示。
问题描述:
ASP.NET项目点击登陆后,服务端项目进行登陆校验,成功登陆后,再次访问服务器端项目时,SessionId发生改变,从而被迫强制退出。
问题原因:
ASP.NET项目中未开启Cookie属性(默认为禁止),导致服务器端项目返回的SessionID无法在客户端存储,再次访问服务器端项目时,服务器端项目找不到之前的SessionID,便自动生成新的SessionID。
解决方案:
在引入WCF服务时会在Web.config中生成如下配置信息,在binding 标签中添加 allowCookies=“true” 属性,可以解决服务端Session丢失(更新)问题。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
<sessionState cookieless="false" regenerateExpiredSessionId="true" timeout="30" />
</system.web>
<system.webServer>
<!--略-->
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="XXXService" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00"
sendTimeout="00:10:00" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647" allowCookies="true"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://IP/service/XXXService.svc"
binding="basicHttpBinding" bindingConfiguration="XXXService"
contract="XXXService.XXXService" name="XXXService" />
</client>
</system.serviceModel>
</configuration>