IdentityServer4主题之定义客户端

Defining Clients 定义客户端

客户端表示哪些可以从你的IdentityServer拿到token的应用。

除了一些可能会变化的细节之外,通常情况下你需要为一个客户端定义如下通用的设置:

  • 一个唯一的client id
  • 一个secret(如果需要的话)
  • 被允许与token service的交互(也叫做授权类型,grant type)
  • 一个网络位置,其中标识和/或访问令牌被发送到(称为重定向URI)的地方
  • 客户端被允许访问的范围(即资源)列表

在运行时,定义的clients(客户端列表)通过一个实现了IClientStore的对象来进行访问。允许从任何数据源加载他们(clients)。在这个文档中我们使用的是内存中的实现。你可以在StartUp类中的ConfigureService方法中调用AddInmemoryClients扩展方法来进行注册服务。

Defining a client for server to server communication 定义一个客户端作为服务到服务的通信

这个场景下不存在交互的用户,就是一个服务(也叫客户端)想要访问一个Api(也叫范围)。

public class Clients
{
    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "service.client",
                ClientSecrets = { new Secret("secret".Sha256()) },

                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = { "api1", "api2.read_only" }
            }
        };
    }
}

Defining browser-based JavaScript client (e.g. SPA) for user authentication and delegated access and API  定义基于浏览器的JavaScript客户端(例如SPA),用于用户身份验证、委托访问和API

这种客户端从javascript利用所谓的implicit flow流程(属于OAuth2.0的范畴,共有四种流程分别是Aothorization code、Implicit、ResourceOwnerPassword、ClientCredential,请参考相应文档)来请求一个id token和access token:

var jsClient = new Client
{
    ClientId = "js",
    ClientName = "JavaScript Client",
    ClientUri = "http://identityserver.io",

    AllowedGrantTypes = GrantTypes.Implicit,
    AllowAccessTokensViaBrowser = true,

    RedirectUris =           { "http://localhost:7017/index.html" },
    PostLogoutRedirectUris = { "http://localhost:7017/index.html" },
    AllowedCorsOrigins =     { "http://localhost:7017" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,

        "api1", "api2.read_only"
    }
};

Defining a server-side web application (e.g. MVC) for use authentication and delegated API access  定义一个服务器端web应用程序(例如MVC),用于使用身份验证和委托的API访问

交互式服务器端(或本机桌面/移动)应用程序使用混合流(Authorization code和Implicit混合)。这种流提供了最好的安全性,因为存取令牌仅通过反向通道(back-channel)调用传输(并且允许您访问刷新令牌):

var mvcClient = new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    ClientUri = "http://identityserver.io",

    AllowedGrantTypes = GrantTypes.Hybrid,
    AllowOfflineAccess = true,
    ClientSecrets = { new Secret("secret".Sha256()) },

    RedirectUris =           { "http://localhost:21402/signin-oidc" },
    PostLogoutRedirectUris = { "http://localhost:21402/" },
    FrontChannelLogoutUri =  "http://localhost:21402/signout-oidc",

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,

        "api1", "api2.read_only"
    },
};

猜你喜欢

转载自www.cnblogs.com/pangjianxin/p/9278387.html