REST WCF 服务和会话在 ASP.NET 中

发布于 2024-10-09 07:06:30 字数 1256 浏览 6 评论 0原文

如果可以的话请帮忙。

我一直在尝试从 WCF REST 服务中访问 asp.net 应用程序的当前会话对象。

根本没有成功。从服务访问的会话对象与 aspx 页面中的会话对象不同。

所以,这是我的问题:是否可以通过 HttpContext.Current.Session 访问 REST WCF 服务中的当前会话?

代码有以下几点:

 [AspNetCompatibilityRequirements
(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)] // I have also tried Required
public class DataService : IDataService

在web.config中:

<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="ClosedRoom.DataServiceBehavior">
     <enableWebScript />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" >
  <baseAddressPrefixFilters>
        <add prefix="http://localhost:63399"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>

  <services>
    <service name="ClosedRoom.DataService">
      <endpoint address="" behaviorConfiguration="ClosedRoom.DataServiceBehavior"
        binding="webHttpBinding" contract="ClosedRoom.IDataService" />
    </service>
  </services>
</system.serviceModel>

谢谢,

Please help if you can.

I have been trying to access the current session object of an asp.net application from within a WCF REST service.

There has been no success at all. the session object accessed from the service is not the same one in the aspx pages.

So, here is my question: Is it possible to access the current session in a REST WCF service through HttpContext.Current.Session ?

The code has the following points:

 [AspNetCompatibilityRequirements
(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)] // I have also tried Required
public class DataService : IDataService

in web.config:

<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="ClosedRoom.DataServiceBehavior">
     <enableWebScript />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" >
  <baseAddressPrefixFilters>
        <add prefix="http://localhost:63399"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>

  <services>
    <service name="ClosedRoom.DataService">
      <endpoint address="" behaviorConfiguration="ClosedRoom.DataServiceBehavior"
        binding="webHttpBinding" contract="ClosedRoom.IDataService" />
    </service>
  </services>
</system.serviceModel>

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-10-16 07:06:30

为了重新恢复会话,您需要提供密钥。在普通的 ASP.NET 应用程序中,该密钥由用户通过 cookie 或 url 参数提供。

您打算如何从 REST 客户端获取该密钥?这些客户端在身份验证后最初如何获取该密钥?他们把钥匙存放在哪里?

这就是为什么大多数基于 REST 的服务都采用 API 访问密钥和另一个密钥来签署每个请求。

恕我直言,会话与基于 REST 的设计无关。

In order for a session to be rehidrated, you need to supply a key. In a normal asp.net application that key is supplied by user either via cookie or url parameter.

How are you planning to acquire that key from the REST client? How those clients get that key initially after the authentication? Where they store the key?

This is why most of the REST based services take a api access key and also another key to sign every request.

IMHO sessions are irrelevant in REST based designs.

不交电费瞎发啥光 2024-10-16 07:06:30

我知道这个问题很久以前就被问到了,但这可以通过在 ASP.NET 应用程序中托管 WCF Restful 服务来实现,然后在服务类的顶部添加以下属性:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

这可以实现许多功能,包括:

HttpContext:在 ASP.NET 兼容模式下运行的 WCF 服务可以访问 Current 及其关联状态。

请参阅此处了解详细信息:AspNetCompatibilityRequirements 的真正含义是什么?

I know this question was asked a long time ago but this can be achieved by hosting the wcf restful service within an asp.net application and then on the top of your service class add the following attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

This enables many things including:

HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state.

See here for more info: What does AspNetCompatibilityRequirements really mean?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文