【问题标题】:WCF - Get Current User IdentityWCF - 获取当前用户身份
【发布时间】:2018-11-15 07:01:35
【问题描述】:

我有一个向服务器发送调用的 Silverlight 应用程序。服务器使用 WCF 服务来执行调用。如果执行呼叫的用户是我期望的用户,我希望能够检查“AfterReceiveRequest”功能。如果不是 - 我想中止操作。

问题是 - 我无法正确获取 IIS 用户名。 我试过这个属性:

OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

只有当我从 Visual Studio 运行应用程序时,他们才给我正确的用户名,而不是在应用程序池上。当我在应用程序池上运行应用程序时,我在这两个参数中都得到了值:IIS APPPOOL\MyApplicationPool。

客户端和服务器在同一台机器上,所以这看起来不像是双跳。

有人可以帮忙吗?我需要它在 Windows 和基本身份验证中工作。

【问题讨论】:

  • 据我所知,您需要使用 Windows 身份验证。使用 Windows 身份验证时,您需要一个 windows 用户名。您需要使用 Windows 身份验证来处理此操作。
  • 首先 - 即使使用 Windows 身份验证,上述参数的值也不正确。此外 - 我看不出有任何理由不使用基本身份验证......

标签: c# wcf iis


【解决方案1】:

这些标识值来自用于 WCF 的身份验证方法。您应该发布您的 WCF 服务器端和客户端配置。要启用基于 Windows 身份验证使用加密握手的 Windows 身份验证,请以本文为例:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-secure-a-service-with-windows-credentials

完成此操作后,您将能够在此处看到 Windows 标识:

ServiceSecurityContext.Current.PrimaryIdentity

关键是在服务器端的绑定级别开启安全模式。

在代码中:

    var myBinding = new WSHttpBinding();
    myBinding.Security.Mode = SecurityMode.Message;

在配置中:

<bindings>  
  <wsHttpBinding>  
   <binding name = "wsHttpBinding_Calculator">  
     <security mode="Message">  
       <message clientCredentialType="Windows"/>  
     </security>  
    </binding>  
  </wsHttpBinding>  
</bindings> 

我认为 clientCredentialType="Windows" 甚至没有必要,因为如果 没有指定,你仍然可以获取windows用户身份。

回应您关于 VS 与 IIS 的观点 - 这应该不是问题。我要说的是,IIS 服务以某种方式无法访问 Active Directory 域。这可能是权限问题。但是,默认情况下 IIS 应该以与 Visual Studio 相同的方式工作,因为 Visual Studio 只使用 IIS Express。如果 IIS 配置为使用与 IIS Express 相同的用户名,则它应该有权以相同的方式通过域进行身份验证。注意:ServiceSecurityContext.Current.PrimaryIdentity 不是 IIS 用户名。它是 Windows 用户名。

【讨论】:

  • 这对我很有帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多