【问题标题】:Retrieve all the roles (groups) a user is a member of in ASP.Net Core API在 ASP.Net Core API 中检索用户所属的所有角色(组)
【发布时间】:2019-07-07 05:47:35
【问题描述】:

我正在尝试检索用户所在的所有角色。

这个在我的本地 IIS 上工作正常,因为我是登录的那个,但是当我将 API 放到服务器上时,它检索的不是我的角色。

    [Authorize(Roles = "Admin")]
    [HttpGet]
    public IActionResult Get()
    {
        WindowsIdentity user = WindowsIdentity.GetCurrent();

        var userRoles = from id in user.Groups
                         select id.Translate(typeof(NTAccount)).Value;

        return Ok(userRoles);
    }

如果我将 WindowsIdentity 更改为 IIdentity,我会在本地和服务器上获得正确的用户,但我无法访问角色。

IIdentity user = User.Identity;
return Ok(user);

如何检索用户访问 API 的所有角色列表?

【问题讨论】:

    标签: asp.net asp.net-core windows-authentication asp.net-core-webapi


    【解决方案1】:

    结果我非常接近,如果有人需要它很简单。在第一个示例中替换这个:

    WindowsIdentity user = WindowsIdentity.GetCurrent();
    

    用那个:

    var user = User.Identity as WindowsIdentity;
    

    【讨论】:

      猜你喜欢
      • 2010-10-20
      • 2017-09-19
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 2013-10-29
      • 2023-03-21
      相关资源
      最近更新 更多