IdentityServer4【Topic】之保护APIs
Protecting APIs 保护api
默认情况下IdentityServer将access token发布成JWT(json web token)格式的。
现在,每个相关的平台都支持验证JWT令牌,这里可以找到一个很好的JWT库列表。流行的库如:
- JWT bearer authentication handler for ASP.NET Core
- JWT bearer authentication middleware for Katana
- IdentityServer authentication middleware for Katana
- jsonwebtoken for nodejs
保护基于Asp.net core的api需要做的事情就是在DI中配置jwt bearer authentication handler。并且在管道上面添加authentication中间件:
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();
}
}
The IdentityServer authentication handler
我们的身份验证处理程序(指IdentityServer4中提供的handler)与上面的处理程序(handler)有相同的用途(实际上它在内部使用Microsoft JWT库),但是添加了一些额外的特性:
- support for both JWTs and reference tokens //同时支持JWTs和引用token
- extensible caching for reference tokens//为引用token支持缓存
- unified configuration model//统一了配置模型
- scope validation//范围的检查
对于最简单的场景,我们的handler配置过程看起来和上面的代码片段非常相似:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//api保护是在api里面做的,需要引入IdentityServer4.AccessTokenValidation这个nuget包。比如下面这个参数来自包中
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();
}
}
Supporting reference tokens支持引用token
如果进来的令牌不是JWT,我们的中间件将会联系发现文档(discovery document,就是./well-known/openid-configuration端点)中找到的内省端点(introspection endpoint)来验证令牌。由于自省端点需要认证,所以你需要提供配置的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(); // that's the default
})
//处理程序将使用在DI容器中注册的任何i分布式缓存实现(例如标准的memory分布式缓存)。
Validating scopes检查请求的范围
ApiName属性(上面的代码提到的这个属性)检查传过来的token的audience(或者叫做aud)的claim,查看是否匹配。
在IdentityServer中你可以将API继续细分成多个范围。如果你需要这种粒度的分化那么你可以使用ASP.NET Core 中的授权策略(authorization policy system)来检查scope:
- 创建全局的策略(基于MVCOpitons进行配置,添加到过滤器上)
services.AddMvcCore(options =>
{
// require scope1 or scope2。create是扩展方法,在内部
//调用了requireclaim。
var policy = ScopePolicy.Create("scope1", "scope2");
options.Filters.Add(new AuthorizeFilter(policy));
})
.AddJsonFormatters()
.AddAuthorization();
- 构建一个范围策略(基于AuthorizationOptions进行构建)
services.AddAuthorization(options =>
{
options.AddPolicy("myPolicy", builder =>
{
// 来自identityserver的扩展方法
builder.RequireScope("scope1");
// and require scope2 or scope3
builder.RequireScope("scope2", "scope3");
});
});
IdentityServer4【Topic】之保护APIs的更多相关文章
- IdentityServer4【QuickStart】之使用ClientCredentials流程保护API
使用ClientCredentials流程保护API 这个示例展示了使用IdentityServer中保护APIs的最基本的场景. 在这个场景中我们会定义一个API和一个想要访问它的客户端.客户端会在 ...
- IdentityServer4系列之中文文档及实际项目经验分享
0.前言 原文:http://docs.identityserver.io/en/release/声明: 1.目录一至五章节根据IdentityServer英文文档翻译而来,有些内容会根据自己的理解来 ...
- IdentityServer4-主题
一.Startup 二.定义Resources 三.定义Clients 四.登录 五.使用外部身份提供商登录 六.Windows身份验证 七.登出 八.注销外部身份提供商 九.联合注销 十.联合网关 ...
- IdentityServer-Protecting an API using Client Credentials
使用客户凭证保护API 这篇快速开始将展示使用IdentityServer保护APIs的最基本使用场景. 在此场景中我们将定义一个API和一个要访问此API的客户端. 客户端将向IdentitySer ...
- IdentityServer4-快速入门
一.设置和概述 二.使用客户端凭证保护API 三.使用密码保护API 四.使用OpenID Connect添加用户验证 五.添加对外部认证的支持 六.切换到Hybrid Flow并添加API访问权限 ...
- 【IdentityServer4文档】- 使用客户端凭据保护 API
使用客户端凭据保护 API quickstart 介绍了使用 IdentityServer 保护 API 的最基本场景. 接下来的场景,我们将定义一个 API 和一个想要访问它的客户端. 客户端将在 ...
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...
- IdentityServer4【Topic】Consent
Conset这个概念在Identityserver4中是表示要当前用户对第三方应用对资源请求的一个确认,它会被做成一个页面. 术语映射: Consent page--确认页面,我喜欢叫做Consent ...
- IdentityServer4【QuickStart】之使用ResourceOwnerPassword流程来保护API
使用ResourceOwnerPassword流程来保护API OAuth2.0中的ResourceOwnerPassword授权流程允许一个客户端发送username和password到token服 ...
随机推荐
- C语言 统计一篇英文短文中单词的个数
//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #define N 1000 void main(){ ] ...
- C#事件の事件解析
事件(event)是基于windows消息处理机制的类,封装的更好,让开发者无须知道底层的消息处理机制,就可以开发出强大的基于事件的应用程序来.委托(delegate)委托可以理解成为函数指针,不同的 ...
- 从 0 → 1,学习Linux该这么开始!
首先我们还是来普及以下概念,讲点虚的.现在是图形系统的天下,windows我们用了20多年.成功归功与它图形界面,你会点鼠标吗你会敲键盘吗?所以你会上网会聊天会玩游戏了.那么,0基础接触的Linux, ...
- 单片机与android手机通信(控制LED小灯亮灭)
1.单片机实验板功能设计 为验证数据通信内容,让单片机板上的四个按键与android手机客户端上的四个LED灯相互控制:为达到上述基本实验要求,采用单字符传输数据即可,硬件需设计两块相同的单片机电路板 ...
- Python之Requests库的异常
异常 说明 requests.ConnectionError 网络链接错误一场,如 ...
- Doc2vec实现原理
论文来源:https://www.eecs.yorku.ca/course_archive/2016-17/W/6412/reading/DistributedRepresentationsofSen ...
- 机器学习算法总结(四)——GBDT与XGBOOST
Boosting方法实际上是采用加法模型与前向分布算法.在上一篇提到的Adaboost算法也可以用加法模型和前向分布算法来表示.以决策树为基学习器的提升方法称为提升树(Boosting Tree).对 ...
- ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例 2012年04月27日 16:59:16 奋斗的小壁虎 阅读数:4500 ...
- django一般架构思维导图
本例已django项目名称为myblog,其下面有2个应用blog和comment介绍:
- if选择语句与switch选择语句的比较、区别及应用场景
if选择语句和switch选择语句的比较: 1.switch语句只支持常量值相等的分支判断,而if语句支持更为灵活,任意布尔表达式均可: 2.switch语句通常比一系列嵌套if语句效率更高:逻辑更加 ...