首页
学习
活动
专区
圈层
工具
发布

IdentityServer Topics(3)- 定义客户端

客户端代表可以从您的身份服务器请求令牌的应用程序。

细节有所不同,但您通常为客户端定义以下常用设置:

  • 一个唯一的客户端ID
  • 一个密钥,如果需要
  • 允许与令牌服务的交互(称为授权类型)
  • 身份或访问令牌被发送到的网络位置(称为重定向URI)
  • 允许客户端访问的范围列表(资源)

在运行时,客户端通过IClientStore的实现来检索。 这允许从配置文件或数据库的任意数据源加载它们。 对于本文档,我们将使用客户端存储的内存存储版本。 您可以通过AddInMemoryClients扩展方法在ConfigureServices中配置内存存储。

定义服务器到服务器通信的客户端

在这种情况下,不存在交互式用户 - 服务(客户端)想要与API(作用域)进行通信:

代码语言:javascript
复制
public class Clients
{
    public static IEnumerable<Client> Get()
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "service.client",
                ClientSecrets = { new Secret("secret".Sha256()) },

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

定义基于浏览器的JavaScript客户端(例如SPA)进行用户认证和授权访问和API

这个客户端使用implicit flow来从JavaScript请求身份和访问令牌:

代码语言:javascript
复制
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"
    }
};

定义服务器端Web应用程序(例如MVC)以进行使用验证和授权的API访问

交互式服务器端(或本地桌面/移动)应用程序使用混合流程(hybrid flow)。 这个流程为您提供了最好的安全性,因为访问令牌仅通过反向通道传输(并允许您访问刷新令牌):

代码语言:javascript
复制
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"
    },
};
下一篇
举报
领券