【问题标题】:Asp.net session doesn't seem to work with WCF WebHttpBindingAsp.net 会话似乎不适用于 WCF WebHttpBinding
【发布时间】:2023-04-06 12:47:01
【问题描述】:

我们正在做一个 WCF REST json 服务 (WebHttpBinding)。由于纯 WCF 会话不适用于这种绑定,因此我们使用的是 asp.net 会话。所以我们设置:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

在我们的 web.config 中,并且:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

在我们的服务实施之上。 我们有几种服务方法,例如:

public void OpenSession(string userName)
public void GetSomething(int somethingId)
public void CloseSession()

然后我们在每个方法中测试HttpContext的sessionId。

HttpContext.Current.Session.SessionID

问题是:它总是在变化。 如果我在网络浏览器中调用 OpenSession?userName='toto',然后再调用 GetSomething?somethingId=1234,会话 ID 会发生变化。

wcf 似乎没有很好地处理 cookie。如果我在 fiddler 中调用 OpenSession 方法,则在 http 标头中,服务没有返回“Set-Cookie”。

【问题讨论】:

    标签: wcf session webhttpbinding


    【解决方案1】:

    我发现:

    默认情况下,WCF 没有 cookie 在客户端启用。所以当 服务器需要Cookie,你需要 打开绑定上的cookie 通过物业为客户 HttpTransportBindingElement.AllowCookies。

    在这篇文章中。检查它是否对你有用:http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx

    【讨论】:

    • 它返回空值。可能是因为启用了 ASP 兼容性。
    • 问题不在客户端。在我的案例中,客户端是智能手机网络浏览器或桌面网络浏览器。 Cookie 已在其中启用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2023-04-06
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多