zoukankan      html  css  js  c++  java
  • 第19章 定义资源

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

    注意
    您可以使用C#对象模型定义资源 - 或从数据存储加载它们。IResourceStore的一个实现处理这些低级细节。对于本文档,我们使用内存实现。

    19.1 定义身份资源

    身份资源是用户的用户ID,名称或电子邮件地址等数据。标识资源具有唯一名称,您可以为其分配任意声明类型。然后,这些声明将包含在用户的身份令牌中。客户端将使用该scope参数来请求访问标识资源。

    OpenID Connect规范指定了几个标准身份资源。最低要求是,您为用户发送唯一ID提供支持 - 也称为主题ID。这是通过公开名为的标准身份资源来完成的openid

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

    该IdentityResources类支持的规范(OpenID,电子邮件,个人资料,电话和地址)中定义的所有范围。如果要全部支持它们,可以将它们添加到支持的身份资源列表中:

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

    19.2 定义自定义标识资源

    您还可以定义自定义标识资源。创建一个新的IdentityResource类,为其指定名称和可选的显示名称和描述,并定义在请求此资源时应将哪些用户声明包含在标识令牌中:

    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
        };
    }
    

    有关身份资源设置的更多信息,请参考

    19.2 定义API资源

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

    要获取API的访问权限,您还需要将它们注册为范围。这次范围类型是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"
                    }
                }
            }
        };
    }
    

    有关API资源设置的更多信息,请参考

    注意
    由资源定义的用户声明由IProfileService扩展点加载。

    github地址

  • 相关阅读:
    Executors 构建线程池
    结构型模式——Bridge(未完成)
    结构型模式——Adapter
    创建型模式——Builder
    创建型模式——Abstract Factory
    Java与线程
    Java内存模型
    类加载
    Class类文件的结构
    垃圾收集器
  • 原文地址:https://www.cnblogs.com/thinksjay/p/10774826.html
Copyright © 2011-2022 走看看