gpt4 book ai didi

wcf - 在以 ASP.NET 兼容模式运行的 WCF 服务中,HttpContext 为空

转载 作者:行者123 更新时间:2023-12-02 00:37:20 24 4
gpt4 key购买 nike

我有一个托管 WCF 服务的 asp.net 网站。然后从桌面应用程序访问此服务。在我的服务中,在 UserNamePasswordValidator 类的实现中执行 Validate 方法期间,HttpContext 始终为 null。我使用用户名作为客户端凭据类型。我需要访问 http 上下文以获取访问服务的 Url,以便正确验证用户名和密码,因为可以使用不同的 Urls 访问该站点,并且每个 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 上下文不应为空的链接

谁能帮我解决这个问题?我宁愿不必将网站的 Url 放入我所有网站的 appSettings 配置部分。

最佳答案

问题是您想要从 Validate 方法访问 HttpContext。据我了解,内部 WCF 实现 Validate 方法在不同的线程中运行。按照设计,此线程无权访问任何可用于处理请求的主线程的上下文。在 Validate 方法中,您无法访问任何基于 WCF 的上下文(OperationContext、ServiceSecurityContext 等),因此我认为它与 HttpContext 相同。

关于wcf - 在以 ASP.NET 兼容模式运行的 WCF 服务中,HttpContext 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com