zoukankan      html  css  js  c++  java
  • 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这个扩展点来加载(也就是可以加载一些我们自定义的声明)。

  • 相关阅读:
    2018 ACM 网络选拔赛 徐州赛区
    2018 ACM 网络选拔赛 焦作赛区
    2018 ACM 网络选拔赛 沈阳赛区
    poj 2289 网络流 and 二分查找
    poj 2446 二分图最大匹配
    poj 1469 二分图最大匹配
    poj 3249 拓扑排序 and 动态规划
    poj 3687 拓扑排序
    poj 2585 拓扑排序
    poj 1094 拓扑排序
  • 原文地址:https://www.cnblogs.com/pangjianxin/p/9278336.html
Copyright © 2011-2022 走看看