IdentityServer Topics(3)- 定义客户端

客户端指可以从你的 identityserver 请求令牌的应用程序。

细节可能有所不同,但是客户端通常有以下设置

  • 一个唯一的客户端ID
  • 一个密钥(非必须)
  • 允许与令牌服务的交互(称为授权类型)
  • 身份或访问令牌被发送到的url(称为重定向URI)
  • 允许客户端访问的Scope列表(API资源)

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

一.定义Server到Server的客户端#

在这种情况下,没有交互式用户 - 服务(也称为客户端)想要与API(aka范围)进行通信:

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

二.定义 avaScript客户端(例如SPA)进行用户认证和授权访问和API#

这个客户端使用implicit flow来从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)。 这个流程为您提供了最好的安全性,因为访问令牌仅通过反向通道传输(并允许您访问刷新令牌):

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

作者:晓晨Master(李志强)

出处:https://www.cnblogs.com/stulzq/p/8144247.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   晓晨Master  阅读(5749)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2015-12-29 Sql Server利用游标批量清空数据表
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示