【问题标题】:How do I access username on the server side using Windows authentication on a WCF service?如何在 WCF 服务上使用 Windows 身份验证访问服务器端的用户名?
【发布时间】:2011-12-13 14:59:37
【问题描述】:

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

ServiceSecurityContext.Current.WindowsIdentity

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity;

这是我在 stackoverflow 上找到的类似问题的答案,但无法编译。 我得到一个“ServiceSecurityContext 不包含 Current 的定义”异常。

那么如何在服务器端获取用户呢?

【问题讨论】:

    标签: c# wcf silverlight wcf-ria-services windows-authentication


    【解决方案1】:
    public static string GetUserIdentity()
    {
       return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
    }
    

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

    【讨论】:

      【解决方案2】:

      试试

      HttpContext.Current.User;
      

      希望对你有帮助

      【讨论】:

      • 这仅在服务已设置为 ASP.NET 兼容模式时才有效。
      • 真的吗?这不是从请求标头中获取用户名吗?
      • 根据:msdn.microsoft.com/en-us/library/aa702682.aspx“HttpContext:从 WCF 服务中访问时,Current 始终为 null。请改用 RequestContext。” ...除非您的服务使用 ASP.NET Compatability 进行装饰
      • 来自同一篇文章:需要传统上由 HTTP 管道提供的功能的 WCF 应用程序应考虑使用独立于主机和传输的 WCF 等效项:OperationContext 而不是 HttpContext。
      • 我试过了,效果很好。我的 wcfService 是在与 silverlight 网络主机相同的项目中创建的,所以我假设它自动设置为 ASP.NET 兼容模式
      猜你喜欢
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多