【问题标题】:How do I list the requesting user's roles in a WCF service?如何在 WCF 服务中列出请求用户的角色?
【发布时间】:2010-10-16 08:57:19
【问题描述】:

刚刚开始掌握 WCF 安全性。如何在服务中列出用户的角色?

例如

// Could use declarative security here, i.e. using PrincipalPermission attribute
public string MyService()
{
    // Would like some code that does something like:
    foreach( Role role in CurrentUser.Roles )
    {
    }
}

谢谢

【问题讨论】:

    标签: c# .net wcf security-roles


    【解决方案1】:

    .NET 中基于角色的安全基础架构(即 IPrincipal)不允许获取用户的所有角色。您只能查询用户是否属于特定角色(通过 IPrincipal.IsInRole("role-name"))。

    但是,如果您不介意被绑定到特定的身份验证/授权设置,可以使用一些解决方案。例如,另一位发帖人指出了在使用 Windows 身份验证时如何获取用户的角色。

    【讨论】:

      【解决方案2】:

      在处理 Windows 组时,您可以使用以下代码:

      foreach (IdentityReference idRef in WindowsIdentity.GetCurrent().Groups)
      {
          Console.WriteLine(idRef.Translate(typeof(NTAccount)).Value);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 2018-04-10
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多