从 WCF Web 服务访问 HttpContext.Current

编程入门 行业动态 更新时间:2024-10-25 06:32:11
本文介绍了从 WCF Web 服务访问 HttpContext.Current的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚开始在 ASP.NET AJAX 中使用 WCF 服务.我从 Javascript 实例化我的 WCF 服务,然后将字符串变量作为参数传递给我的 WCF 服务方法(带有 OperationContract 签名).然后我返回一个绑定到我的自定义 Javascript 类的 .NET 对象(用 DataContract 定义).我无法根据登录到我的 Web 会话的用户进行身份验证.但是,WCF Web 服务是一个完全不同的服务,没有 HttpContext.Current 对象的上下文.访问该对象的最安全方法是什么?

解决方案

您可以通过启用 AspNetCompatibility 来访问 HttpContext.Current,最好通过配置:

<system.serviceModel><serviceHostingEnvironment aspNetCompatibilityEnabled="true"/></system.serviceModel></配置>

这反过来又允许您访问当前用户:HttpContext.Current.User - 这就是您所追求的,对吧?

您甚至可以通过使用附加属性装饰您的服务类来强制执行 AspNetCompatibility:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

(在 System.ServiceModel.Activation 命名空间中.)如果该属性到位,除非启用 AspNetCompatibility,否则您的服务将无法启动!

I just started using WCF Services with ASP.NET AJAX. I instantiate my WCF service from Javascript and then pass string variables as arguments to my WCF Service method (with an OperationContract signature). I then return a .NET object (defined with a DataContract) which is bound to my custom Javascript class. I'm having trouble authenticating based on the user logged into my web session. However, the WCF web service is a completely different service with no context to the HttpContext.Current object. What is the most secure way to get access to that object?

解决方案

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration:

<configuration> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> </system.serviceModel> </configuration>

That in turn allows you to get access to the current user: HttpContext.Current.User - which is what you're after, right?

You can even enforce AspNetCompatibility by decorating your service class with an additional attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

(In the System.ServiceModel.Activation namespace.) If that attribute is in place, your service will fail to start unless AspNetCompatibility is enabled!

更多推荐

从 WCF Web 服务访问 HttpContext.Current

本文发布于:2023-11-08 14:18:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Web   WCF   Current   HttpContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!