IdentityServer Topics(2)- 定义资源

您通常在系统设计中的第一件事就是您要保护的资源。 这可能是您的用户的身份信息,如个人资料数据或电子邮件地址,或访问API。

您可以使用C#对象模型定义资源(硬编码),或从数据存储中加载它们。 IResourceStore的实现处理这些低级细节。 本文使用的是in-memory的实现。

定义身份资源

身份资源也是数据,如用户ID,姓名或用户的电子邮件地址。 身份资源具有唯一的名称,您可以为其分配任意身份信息单元(比如姓名、性别、身份证号和有效期等都是身份证的身份信息单元)类型。 这些身份信息单元将被包含在用户的身份标识(Id Token)中。 客户端将使用scope参数来请求访问身份资源。

OpenID Connect规范指定了一对标准的身份资源。 最低要求是,您提供支持为您的用户颁发一个唯一的ID - 也称为subject id(sid)。 这是通过暴露称为openid的标准身份资源完成的:

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResources.OpenId()
    };
}

IdentityResources类支持定义规范中的所有作用域(scope)(openid,email,profile,电话和地址)。 如果您想全部支持,可以将它们添加到支持的身份资源列表中:

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Email(),
        new IdentityResources.Profile(),
        new IdentityResources.Phone(),
        new IdentityResources.Address()
    };
}

定义自定义身份资源

您还可以定义自定义身份资源。 创建一个新的IdentityResource类,为其指定一个名称和一个可选的显示名称和描述,并在请求此资源时定义哪个用户身份单元应该包含在身份令牌(Id Token)中:

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    var customProfile = new IdentityResource(
        name: "custom.profile",
        displayName: "Custom profile",
        claimTypes: new[] { "name", "email", "status" });

    return new List<IdentityResource>
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
        customProfile
    };
}

定义API资源

为了允许客户请求API的访问令牌,您需要定义API资源,例如:

要访问API的令牌,还需要为其注册作用域(Scope)。 这次作用域类型是Resource类型的:

public static IEnumerable<ApiResource> GetApis()
{
    return new[]
    {
        // simple API with a single scope (in this case the scope name is the same as the api name)
        new ApiResource("api1", "Some API 1"),

        // expanded version if more control is needed
        new ApiResource
        {
            Name = "api2",

            // secret for using introspection endpoint
            ApiSecrets =
            {
                new Secret("secret".Sha256())
            },

            // include the following using claims in access token (in addition to subject id)
            UserClaims = { JwtClaimTypes.Name, JwtClaimTypes.Email },

            // this API defines two scopes
            Scopes =
            {
                new Scope()
                {
                    Name = "api2.full_access",
                    DisplayName = "Full access to API 2",
                },
                new Scope
                {
                    Name = "api2.read_only",
                    DisplayName = "Read only access to API 2"
                }
            }
        }
    };
}

装载用户身份单元资源由IProfileService实现来完成。

作者:晓晨Master(李志强)

出处:https://www.cnblogs.com/stulzq/p/8144185.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   晓晨Master  阅读(6905)  评论(6编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2015-12-29 Sql Server利用游标批量清空数据表
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示