在WCF中使用Session WS2007FederationHttpBinding(WSFederationHttpBinding)

时间:2013-07-12 08:37:12

标签: asp.net wcf wcf-binding ws-federation wsfederationhttpbinding

使用 WS2007FederationHttpBinding 时,我无法将我的值存储在WCF 会话中。我已经看过这个话题Session in WCF not consistent,但它没有帮助。此外,我不关心可靠的会话,我只想在会话中存储一些信息并在第二次请求时读取它。这个方案适用于basicHttpsBinding。

这是我的约束力。创建频道后,我将其存储到客户端会话

 var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential);
 binding.Security.Message.EstablishSecurityContext = true;
 binding.Security.Message.IssuedKeyType = SecurityKeyType.BearerKey;
 binding.MaxReceivedMessageSize = 4000000;
 var serviceFactory = new ChannelFactory<T>(binding, new EndpointAddress(serviceUrl));
 serviceFactory.Credentials.SupportInteractive = false;
 var channel = serviceFactory.CreateChannelWithIssuedToken(token);

服务配置:     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

服务中的会话访问:

HttpContext.Current.Session["serviceSession"] = "123";

服务中的会话检索(在第二个请求中始终为null,但 SessionId 是相同的):

if (HttpContext.Current.Session["serviceSession"] == null) ...

我将频道本身保留在两个请求之间的客户端会话中,并重新从会话中获取第二个请求。

Session["clientSession"] = channel;

1 个答案:

答案 0 :(得分:0)

我相信HttpContext.Current.Session只有在服务上配置asp兼容模式时才有效。但即便如此,它也不会与您的联合设置相关联。对我有用的是按会话定义服务实例模式:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
public class Service : IService

然后您可以使用服务类的本地数据成员在同一会话的不同调用之间保留数据。

相关问题