• IdentityServer4【Topic】之定义资源


    Defining Resources 定义资源

    你在系统中通常定义的第一件事是你想要保护的资源。这可能是你的用户的身份信息,比如个人资料数据或电子邮件地址,或者访问api。

    你可以通过C#对象模型(内存中的)--或者加载数据库资源(数据库中的)来定义资源。一个IResouceStore的实现来处理这些底层的细节。对于这篇文档来说我们使用的是基于内存中的实现。

    Defining identity resources 定义IdentityResource

    一个用户的ID、名字、邮件地址等这些信息,都可以看成是资源,有一个更好的名字是Identity resource,还有一个api resource,后面会讲。这两种都被IdentityServer4当作了资源。一个是用户范畴的,另一个是api范畴的。一个Identity resource有一个唯一的名字作为标识符,并且你可以给一个identity resource赋予任意类型的声明(claim)类型。这些声明(claim)之后会被包含在一个Identity token(ID token)中。第三方的客户端使用”scope“这个参数来请求一个identity resource。

    OpenID Connect规范中制定了一些标准的identity resource。最基本的要求是你为你的用户放出一个唯一的ID,这个ID通常也叫做subject id。这个过程是通过暴露一个叫做openid的标准identity resource来完成的。

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

    IdentityResources类(注意是复数)涵盖了规范中(上面提到的)定义了的所有范围(openid、email、profile、telephone和address)如果想要支持他们,就在方法中进行引入:

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

    Defining custom identity resources 定义自定义的Identity Resource

    你当然可以定义自定义的Identity Resource。通过new创建一个IdentityResource的类实例,给它一个名字和一些其他的选项比如displayname、description等等并且当这个resource被请求时定义哪些claim可以被包含进Identity 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
        };
    }

    通过这里查看关于IdentityResource的更多信息。

    Defining API resources 定义ApiResource

    如果要允许第三方客户端请求access token(访问令牌)并以此访问客户端,你需要定义ApiResource(api资源),例如:

    为了获得访问api的access token(访问令牌),您还需要将它们注册为一个范围(scope)。这一次,范围类型是资源:

    public static IEnumerable<ApiResource> GetApis()
    {
        return new[]
        {
           //简单的API只有一个scope(下面这个代码中的scope名称就和ApiResource的名称是一样的)
            new ApiResource("api1", "Some API 1"),
    
            // 扩展版本:如果你需要更多的控制
            new ApiResource
            {
                Name = "api2",
    
                // 使用introspection endpoint的密钥
                ApiSecrets =
                {
                    new Secret("secret".Sha256())
                },
    
                // 下面的代码会在访问令牌(access token)中增加除了subject id以外其他的用户声明(claim)
                UserClaims = { JwtClaimTypes.Name, JwtClaimTypes.Email },
    
                // 这个ApiResource定义了两个scope
                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"
                    }
                }
            }
        };
    }

    点击这里查看更多关于ApiResource的信息.

    注意:ApiResource中定义的UesrClaims属性可以由IProfileService这个扩展点来加载(也就是可以加载一些我们自定义的声明)。

  • 相关阅读:
    ideaj项目切换不同版本的jdk
    物理机(window)安装linux系统
    linux jar自启动
    swap扩容
    tomcat加载外部项目
    springboot2.3.2控制台输出@RequestMapping路径
    linux磁盘扩容
    springboot-easycode配置文件修改
    List
    Map HashMap跟HashTable
  • 原文地址:https://www.cnblogs.com/pangjianxin/p/9278336.html
Copyright © 2020-2023  润新知