C# 从 WCF 服务器端获取 Windows 用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/292233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Windows Username from WCF server side
提问by Ana Betts
I'm pretty green with web services and WCF, and I'm using Windows integrated authentication - how do I get the username on the server-side interface? I believe that I'm supposed to implement a custom Behavior, or perhaps something with WCF Sessions? Any clues would be super-handy.
我对 Web 服务和 WCF 非常熟悉,并且我正在使用 Windows 集成身份验证 - 如何在服务器端界面上获取用户名?我相信我应该实现自定义行为,或者可能是 WCF Sessions?任何线索都会非常方便。
采纳答案by Mitch Baker
Here is a snippet of service code that shows how you could retrieve and use the WindowsIdentity associated with the caller of a WCF service.
下面是一段服务代码,展示了如何检索和使用与 WCF 服务调用方关联的 WindowsIdentity。
This code is assuming that you are accepting most of the defaults with your configuration. It should work without any problems with the Named Pipe or the Net TCP binding.
此代码假设您接受大多数配置的默认值。使用命名管道或 Net TCP 绑定时,它应该可以正常工作。
the p.Demand() will determine if the user is in the windows group specified by the permissionGroup variable.
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();
}
回答by Joel Martinez
have you tried WindowsIdentity.GetCurrent();
?
你试过WindowsIdentity.GetCurrent();
吗?
回答by Joe
Try looking at ServiceSecurityContext.Current.WindowsIdentity
尝试查看 ServiceSecurityContext.Current.WindowsIdentity
回答by Francisco Goldenstein
To get the WCF Service caller username:
要获取 WCF 服务调用者用户名:
var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;
var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;