gpt4 book ai didi

wcf - WCF 中的 ServiceSecurityContext.Current 为 null

转载 作者:行者123 更新时间:2023-12-03 04:58:09 25 4
gpt4 key购买 nike

我正在尝试检索 WCF 服务中登录的 Windows 用户。我尝试过使用 ServiceSecurityContext 但 Current 始终为 null。

ServiceSecurityContext.Current.WindowsIdentity.Name

我也尝试过使用OperationContext。在这种情况下,ServiceSecurityContext 返回 null。

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

这是我的 web.config:

 <bindings>
<basicHttpBinding>
<binding name="HttpWindowsBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>

还有

<authentication mode="Windows"/> 

有人能看出我做错了什么吗?

更新:我放弃了让 ServiceSecurityContext 工作的尝试。最后我通过设置aspNetCompatibilityEnabled="true"找到了解决方案。

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

我还在我的服务类中添加了以下属性:

[System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Required)]

这使我能够通过以下方式检索 Windows 用户:

HttpContext.Current.User.Identity.Name

最佳答案

我遇到了同样的错误并设法解决了它,你不需要使用 basichttpBinding。以下是我的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MSADC">
<security mode ="Message">
<transport clientCredentialType="Windows" />
<message establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFAuthentication.WCFAuthentication">
<endpoint bindingConfiguration="MSADC" address="" binding="wsHttpBinding" contract="WCFAuthentication.IWCFAuthentication">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8088" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<appSettings>
<add key="wcf:disableOperationContextAsyncFlow" value="false" />
</appSettings>
</configuration>

关于wcf - WCF 中的 ServiceSecurityContext.Current 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619159/

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