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

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

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

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.Audience = "api1";
});
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseAuthentication();
app.UseMvc();
}
}

29.1 IdentityServer身份验证处理程序

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

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

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

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.ApiName = "api1";
});
} public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseAuthentication();
app.UseMvc();
}
}

你可以从nugetgithub获得包。

29.2 支持引用标记

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

.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = "https://demo.identityserver.io"; // name of the API resource
options.ApiName = "api1";
options.ApiSecret = "secret";
})

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

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

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

29.3 验证范围

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

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

29.3.1 制定全球政策:

services
.AddMvcCore(options =>
{
// require scope1 or scope2
var policy = ScopePolicy.Create("scope1", "scope2");
options.Filters.Add(new AuthorizeFilter(policy));
})
.AddJsonFormatters()
.AddAuthorization();

29.3.2 制定范围政策:

services.AddAuthorization(options =>
{
options.AddPolicy("myPolicy", builder =>
{
// require scope1
builder.RequireScope("scope1");
// and require scope2 or scope3
builder.RequireScope("scope2", "scope3");
});
});

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. oracle基础语句练习

    1. 创建相关表结构 Emp----员工信息表 Ename ), --姓名 Empno ), --编号 Deptno ), --所在部门 Job ), --工种(人员类别),如:manager 经理, ...

  2. RabbitMQRPC 官方demo

    public class RPCServer { public static void Test() { var factory = new ConnectionFactory() { HostNam ...

  3. 2019PHP面试题最全面归纳总结

    1.请选择以下代码运行的结果: <?php if ('1e3' == '1000') echo 'LOL'; ?> A 无任何输出结果  B   LOL  C 不执行且报错 解析:1e3 ...

  4. js计算指定日期的下一年的日期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 3-3 Hadoop集群完全分布式配置部署

    Hadoop集群完全分布式配置部署 下面的部署步骤,除非说明是在哪个服务器上操作,否则默认为在所有服务器上都要操作.为了方便,使用root用户. 1.准备工作 1.1 centOS6服务器3台 手动指 ...

  6. DCOS实践分享(2):基于Docker Compose和Swarm的Docker化之路

    2016 年1 月 23 日,北京史上气温最低的一天. 在下午 1 点半的时候,由 DaoCloud 赞助的 2016 年度首次 Docker Meetup 准时开始. 在这次Meetup中,我分享了 ...

  7. AIO系列文档(2)----TIO使用

    AIO系列文档(1)----图解ByteBuffer中介绍了ByteBuffer用法,下面通过介绍t-io介绍如何使用: hello world例子简介 本例子演示的是一个典型的TCP长连接应用,代码 ...

  8. [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  9. [Swift]LeetCode383. 赎金信 | Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  10. [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...