IdentityServer 默认以JWT(JSON Web令牌)格式发出访问令牌。

今天的每个相关平台都支持验证JWT令牌,这里可以找到一个很好的JWT库列表。热门库例如:

保护基于ASP.NET Core的API只需在DI中配置JWT承载认证处理程序,并将认证中间件添加到管道:

  1. public class Startup
  2. {
  3. public void ConfigureServices(IServiceCollection services)
  4. {
  5. services.AddMvc();
  6. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  7. .AddJwtBearer(options =>
  8. {
  9. // base-address of your identityserver
  10. options.Authority = "https://demo.identityserver.io";
  11. // name of the API resource
  12. options.Audience = "api1";
  13. });
  14. }
  15. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
  16. {
  17. app.UseAuthentication();
  18. app.UseMvc();
  19. }
  20. }

29.1 IdentityServer身份验证处理程序

我们的身份验证处理程序与上述处理程序的用途相同(实际上它在内部使用Microsoft JWT库),但添加了一些其他功能:

  • 支持JWT和参考令牌
  • 用于引用标记的可扩展缓存
  • 统一配置模型
  • 范围验证

对于最简单的情况,我们的处理程序配置看起来非常类似于上面的代码段:

  1. public class Startup
  2. {
  3. public void ConfigureServices(IServiceCollection services)
  4. {
  5. services.AddMvc();
  6. services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
  7. .AddIdentityServerAuthentication(options =>
  8. {
  9. // base-address of your identityserver
  10. options.Authority = "https://demo.identityserver.io";
  11. // name of the API resource
  12. options.ApiName = "api1";
  13. });
  14. }
  15. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
  16. {
  17. app.UseAuthentication();
  18. app.UseMvc();
  19. }
  20. }

你可以从nugetgithub获得包。

29.2 支持引用标记

如果传入令牌不是JWT,我们的中间件将联系发现文档中的内省端点以验证令牌。由于内省端点需要身份验证,因此您需要提供已配置的API密钥,例如:

  1. .AddIdentityServerAuthentication(options =>
  2. {
  3. // base-address of your identityserver
  4. options.Authority = "https://demo.identityserver.io";
  5. // name of the API resource
  6. options.ApiName = "api1";
  7. options.ApiSecret = "secret";
  8. })

通常,您不希望为每个传入请求执行到内省端点的往返。中间件有一个内置缓存,您可以像这样启用:

  1. .AddIdentityServerAuthentication(options =>
  2. {
  3. // base-address of your identityserver
  4. options.Authority = "https://demo.identityserver.io";
  5. // name of the API resource
  6. options.ApiName = "api1";
  7. options.ApiSecret = "secret";
  8. options.EnableCaching = true;
  9. options.CacheDuration = TimeSpan.FromMinutes(10); // that's the default
  10. })

处理程序将使用在DI容器中注册的任何IDistributedCache实现(例如标准的MemoryDistributedCache)。

29.3 验证范围

所述ApiName属性检查该令牌具有匹配观众(或短aud),如权利要求。

在IdentityServer中,您还可以将API细分为多个范围。如果需要该粒度,可以使用ASP.NET Core授权策略系统来检查范围。

29.3.1 制定全球政策:

  1. services
  2. .AddMvcCore(options =>
  3. {
  4. // require scope1 or scope2
  5. var policy = ScopePolicy.Create("scope1", "scope2");
  6. options.Filters.Add(new AuthorizeFilter(policy));
  7. })
  8. .AddJsonFormatters()
  9. .AddAuthorization();

29.3.2 制定范围政策:

  1. services.AddAuthorization(options =>
  2. {
  3. options.AddPolicy("myPolicy", builder =>
  4. {
  5. // require scope1
  6. builder.RequireScope("scope1");
  7. // and require scope2 or scope3
  8. builder.RequireScope("scope2", "scope3");
  9. });
  10. });

github地址

第29章 保护API - Identity Server 4 中文文档(v1.0.0)的更多相关文章

  1. 第9章 使用客户端凭据保护API - Identity Server 4 中文文档(v1.0.0)

    快速入门介绍了使用IdentityServer保护API的最基本方案. 我们将定义一个API和一个想要访问它的客户端. 客户端将通过提供ClientCredentials在IdentityServer ...

  2. 第10章 使用密码保护API - Identity Server 4 中文文档(v1.0.0)

    OAuth 2.0资源所有者密码授权允许客户端向令牌服务发送用户名和密码,并获取代表该用户的访问令牌. 除了无法承载浏览器的旧应用程序之外,规范通常建议不要使用资源所有者密码授予.一般来说,当您要对用 ...

  3. 第62章 EntityFramework支持 - Identity Server 4 中文文档(v1.0.0)

    为IdentityServer中的配置和操作数据扩展点提供了基于EntityFramework的实现.EntityFramework的使用允许任何EF支持的数据库与此库一起使用. 这个库的仓库位于这里 ...

  4. 第39章 引用令牌 - Identity Server 4 中文文档(v1.0.0)

    访问令牌有两种形式 - 自包含或引用. JWT令牌将是一个自包含的访问令牌 - 它是一个带有声明和过期的受保护数据结构.一旦API了解了密钥材料,它就可以验证自包含的令牌,而无需与发行者进行通信.这使 ...

  5. 第36章 扩展授权 - Identity Server 4 中文文档(v1.0.0)

    OAuth 2.0为令牌端点定义了标准授权类型,例如password,authorization_code和refresh_token.扩展授权是一种添加对非标准令牌颁发方案(如令牌转换,委派或自定义 ...

  6. 第27章 联合网关 - Identity Server 4 中文文档(v1.0.0)

    通用架构是所谓的联合网关.在此方法中,IdentityServer充当一个或多个外部身份提供商的网关. 该架构具有以下优点: 您的应用程序只需要了解一个令牌服务(网关),并且屏蔽了有关连接到外部提供程 ...

  7. 第19章 定义资源 - Identity Server 4 中文文档(v1.0.0)

    您通常在系统中定义的第一件事是您要保护的资源.这可能是您的用户的身份信息,如个人资料数据或电子邮件地址,或访问API. 注意 您可以使用C#对象模型定义资源 - 或从数据存储加载它们.IResourc ...

  8. 第61章 IdentityServer Options - Identity Server 4 中文文档(v1.0.0)

    IssuerUri 设置将在发现文档和已颁发的JWT令牌中显示的颁发者名称.建议不要设置此属性,该属性从客户端使用的主机名中推断颁发者名称. PublicOrigin 此服务器实例的来源,例如http ...

  9. 第58章 Profile Service - Identity Server 4 中文文档(v1.0.0)

    IdentityServer通常在创建令牌或处理对userinfo或内省端点的请求时需要有关用户的身份信息.默认情况下,IdentityServer仅具有身份验证cookie中的声明,以便为此身份数据 ...

随机推荐

  1. 转 the best for wcf client

    原文:http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-bl ...

  2. [LeetCode] Binary Gap 二进制间隙

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  3. Python-数据类型1

    在Python中常见的数据类型有:整数(int).字符串(str).小数/浮点数(float).列表.元组.字典和布尔类型等,下面会进行一一介绍. 整数和小数,不用多介绍相信大家都有所了解,字符串是用 ...

  4. Vue 单文件原件 — vCheckBox

    简书原文 做东西一向奉行的是致简原则,一定要让使用者简单 这是我在使用 Vue 一段时间后尝试制作的一个小玩意 我希望可以做一堆这样的小玩意,随意组合使用,感觉挺好的 源码在最后 演示DEMO 示例: ...

  5. 混合物App开发中,在移动设备上调试查看日志,重写window.console

    (function(){ var print={ lock:true, log:function(param){ if(this.lock){ var element=document.createE ...

  6. 【RL-TCPnet网络教程】第3章 初学RL-TCPnet的准备工作及其快速上手

    第3章       初学RL-TCPnet的准备工作及其快速上手 俗话说万事开头难,学习一门新的知识,难的往往不是知识本身,而是如何快速上手,需要什么资料和开发环境.一旦上手后,深入的学习就相对容易些 ...

  7. Linux下源码安装并配置Nginx

    实验环境 一台最小化安装的CentOS 7.3 虚拟机 安装nginx 安装nginx依赖包 yum install -y pcre-devel zlib-devel openssl-devel wg ...

  8. 基本类型数据转换(int,char,byte)

    public class DataUtil { public static void main(String[] args) { int a = 8; int value = charToInt(by ...

  9. [Swift]LeetCode513. 找树左下角的值 | Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  10. [Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences

    Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...