使用 Identity Server 3 提供对用户组的访问

Providing access to User Groups using Identity Server 3

我目前正在处理的应用程序是一个巨大的 ASP.NET MVC Web 应用程序,它使用传统的 Windows 身份验证模型。我们正在尝试使用 Identity Server 3 和 Open ID Connect 迁移到单点登录模型。

关于我的问题,在使用 Identity Server 时,是否有任何 way/work 可以在用户组基础上提供访问权限?这里的问题是我的用户组可以是基于角色的,也可以是活动目录组。我正在寻找类似 InMemoryGroup(类似于 InMemoryUser)的东西。

为可以基于角色或不基于角色的用户组模仿以下内容:

new InMemoryUser
            {
                Username = "harry",
                Password = "thisispassword@123",
                Subject = "1",
                Claims = new Claim[]
                {
                    new Claim(Constants.ClaimTypes.Name, "Harry Potter"),
                    new Claim(Constants.ClaimTypes.GivenName, "Harry"),
                    new Claim(Constants.ClaimTypes.FamilyName, "Potter"),
                    new Claim(Constants.ClaimTypes.Email, "harry.potter@hogwarts.com"),
                    new Claim(Constants.ClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                    new Claim(Constants.ClaimTypes.Role, "Administrator"),
                }
            }

我对 Identity Server 和 Open ID Connect 比较陌生,无法在网上找到任何东西,非常感谢任何线索。

要表示用户是某个组的成员这一事实,您只需添加声明即可。您可以添加:

new Claim("Group", "Hogwarts Student")

致您记忆中的用户。该声明表明 Harry 是 "Hogwarts Student" 组的成员。您需要注意这里发生的几件事。被调用的构造函数是:

new Claim(string claimType, string claimValue)

Identity Server 在常量 class 中提供了一些标准的,但您可以自己编写。另外,你可以有多个组声明,所以你可以有

new Claim("Group", "Hogwarts Student"),
new Claim("Group", "Gryffindor House)

如果您想查看 Harry 是否在 "Gryffindor House" 组中,您只需搜索声明类型等于 "Group" 且声明价值等于的声明列表至 "Gryffindor House".

最后,组和角色之间的差异通常更多地是应用程序语义的问题,而不是它们存储方式的物理差异。通常,可以为应用程序执行 "Administrator" 角色的用户集只是一组用户。正是应用程序对待组用户的方式使该组成为一个角色。