您如何识别WCF中的认证用户?

RSo*_*erg 5 c# asp.net asp.net-mvc wcf

我有一个将使用基本身份验证的WCF服务,并希望能够识别"谁"正在尝试使用该服务.我知道HttpContext.Current是NULL并且在WCF服务中,但是不知道获取用户名的替代方法.

对于网站,我可以使用:

userName = HttpContext.Current.Request.ServerVariables["LOGON_USER"];
Run Code Online (Sandbox Code Playgroud)

如何在WCF服务中获取userName?

Jos*_*osh 8

这样的事可能吗?

string login = OperationContext.Current
                               .ServiceSecurityContext
                               .PrimaryIdentity
                               .Name;
Run Code Online (Sandbox Code Playgroud)

显然,沿着该路径检查空引用异常是有帮助的,但是你明白了.