【发布时间】:2011-05-03 12:06:01
【问题描述】:
我有一个托管 WCF 服务的 asp.net 网站。然后从桌面应用程序访问此服务。在我的服务中,在执行 UserNamePasswordValidator 类的 Validate 方法期间,HttpContext 始终为空。我使用用户名作为客户端凭据类型。我需要访问 http 上下文以获取访问服务的 Url,以便正确验证用户名和密码,因为可以使用不同的 Url 访问该站点,并且每个 URL 都有不同的用户存储。
包含将在验证器类(以及验证器类)之后调用的方法的类的以下属性
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
我有一个服务配置如下:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpSecurityOptions">
<security mode="Message">
<message clientCredentialType="UserName" establishSecurityContext="true" negotiateServiceCredential="true"/>
<transport clientCredentialType="Certificate" proxyCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="SecurityServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFServer.MyAuthenticator" includeWindowsGroups="false"/>
<serviceCertificate findValue="myurl.com" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="SecurityServiceBehavior" name="Test.WCF.Actions">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpSecurityOptions" contract="WCFServer.IActions"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
我见过HttpContext is not initialised on first call bug,但每次调用服务都会发生这种情况,即使我在同一个连接上多次调用同一个方法
编辑:澄清问题以回答 marc_s 的评论和 Aliostad 的问题
编辑:添加了以下链接,建议 http 上下文不应为空
- http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx
- http://msdn.microsoft.com/en-us/library/aa702682(v=VS.90).aspx
有人可以帮我解决这个问题吗?我宁愿不必将网站的 Url 放在我所有网站的 appSettings 配置部分。
【问题讨论】:
-
在您的 Validate 方法中,您需要 HttpContext 做什么??
-
它是在 UserNamePasswordValidator.Validate 中还是在您的 [OperationContract] 方法中为空?我不确定从验证器访问 HttpContext 是否受支持...
-
仅在 UserNamePasswordValidator.Validate 方法中为空。当传输安全性默认为 Windows 时,我在早期的开发版本中使用了此功能(绝对是用户名消息安全性)。 ServiceOperation.Current 在这个阶段应该是空的,但我找不到任何可以确认 HttpContext 也将为空的东西。
标签: wcf wcf-security