我正在尝试在 WCF 服务中检索已登录的 Windows 用户。我试过使用 ServiceSecurityContext 但 Current 始终为空。
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