从WCF服务器端获取Windows用户名

我很喜欢Web服务和WCF,我正在使用Windows集成身份validation – 如何在服务器端接口上获取用户名? 我相信我应该实现一个自定义行为,或者可能是WCF会话的东西? 任何线索都会非常方便。

下面是一段服务代码,展示了如何检索和使用与WCF服务调用者关联的WindowsIdentity。

此代码假定您接受配置的大多数默认值。 它应该在命名管道或Net TCP绑定没有任何问题的情况下工作。

p.Demand()将确定用户是否在permissionGroup变量指定的windows组中。

private static void DemandManagerPermission() { // Verify the use has authority to proceed string permissionGroup = ConfigurationManager.AppSettings["ManagerPermissionGroup"]; if (string.IsNullOrEmpty(permissionGroup)) throw new FaultException("Group permissions not set for access control."); AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); var p = new PrincipalPermission(ServiceSecurityContext.Current.WindowsIdentity.Name, permissionGroup, true); p.Demand(); } 

尝试查看ServiceSecurityContext.Current.WindowsIdentity

要获取WCF服务调用者用户名:

var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

你试过WindowsIdentity.GetCurrent();