IdentityServer4 Resources

原文地址

Resources 的定义

通常在系统中是顶一个需要保护的资源。这些资源可是用户的信息,比如身份信息或者邮箱地址,也可以是某些API的访问权限。

Note: 可以通过C#的对象模型或者通过数据库定义资源。通过实现 IResourceStore 来处理这些低层次的细节。本文章使用 in-memory 的实现方式。

identity resources 的定义

Identity resources 是用户的Id,Name,Email数据。每一个Identity resource都有一个独立的name,并且可以赋任何的claim type值给它。这些claims将会被包含在用户的identity token里面。client 会使用 'scope' 参数去请求访问identity resouce。

OpenID Connect 规范制定了一些标准的Identity resources. 最基本的Identity resource要求提供用户的唯一识别Id-也可以叫做 subject Id. 这可以通过公开名称为 openid 的标准 identity resource 来实现.

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

IdentityResources 支持所有定义在规范中的所有的 scope(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()
    };
}

custom identity resources 的定义

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    var customProfile = new IdentityResource(
        name: "custom.profile", 
        displayName: "Custom profile", // optional
        claimTypes: new[] { "name", "email", "status" });//包含的用户claims

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

更多identity resource的设置内容

API resources 的定义

要让clients请求某些API的access token,需要定义API resource,同时将这些APIs注册为一个scope。这一次 scope type是 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)
        //这种情况下scope name和 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 resource 更多的设置信息

Note: resource定义的user claims 通过 IProfileService 扩展加载。

原文地址:https://www.cnblogs.com/ArvinZhao/p/11341714.html