2011-12-13 52 views
1

我有一个使用Windows身份验证的WCF服务的Silverlight应用程序。 我的用户获得了正确的身份验证。我可以在客户端访问他们的用户名和角色。 但是我如何在服务器端做同样的事情呢? 我已经试过这如何在WCF服务上使用Windows身份验证访问服务器端的用户名?

ServiceSecurityContext.Current.WindowsIdentity 

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity; 

这是我发现的在这里计算器类似的问题的答案,但它不编译。 我得到'ServiceSecurityContext不包含当前'定义的异常。

那么如何让服务器端的用户?

回答

1

尝试

HttpContext.Current.User; 

希望这有助于

+1

如果该服务已被设置为ASP.NET兼容模式这只会工作。 – syneptody

+0

真的吗?这不是从请求头获取用户名吗? –

+0

根据:http://msdn.microsoft.com/en-us/library/aa702682.aspx“HttpContext:当从WCF服务中访问时,Current始终为空,请改用RequestContext。” ...除非您的服务使用ASP.NET兼容 – syneptody

2
public static string GetUserIdentity() 
{ 
    return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name; 
} 

OperationContext位于System.ServiceModel命名空间中。

+1

嗯,我想这一点,但我的ServiceSecurityContext为空.. – grabah

相关问题