针对服务器端的 WCF 调用方的活动目录组进行验证



>我有一个 WCF 服务,托管在 WindowService 中,使用 nettcpbinding 如果发送方属于特定的 AD 组,我想在其中执行 C# 代码的检查。

可能吗?如果可能,如何?

假设 WCF 客户端和服务器位于同一域中,您可以执行以下操作:

在客户端,允许使用 Windows 标识对客户端进行身份验证:

using System.Security.Principal;
....
ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Identification;

在服务器端,检索调用方 Windows 标识并测试它是否属于该组:

    WindowsIdentity callerWindowsIdentity = ServiceSecurityContext.Current.WindowsIdentity;
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(callerWindowsIdentity);
    var isInRole = windowsPrincipal.IsInRole("Users");

最新更新