使用ResourceOwnerPassword流程来保护API

OAuth2.0中的ResourceOwnerPassword授权流程允许一个客户端发送username和password到token服务上面,以便获取一个代表用户的access token。

”规范“中建议只对受信任的客户端使用这种授权类型。通常情况来讲,在有用户交互的场景下,你应该优先使用OpenID Connect协议中的其中一个流程(有authorization code 、implicit、hybrid)来对用户进行认证,并获取access token。

话虽如此,这种授权类型引入了IdentityServer中的用户的概念,这也是我们要展现它的唯一原因。

添加用户

就像内存中的资源(或者叫做scopes)和客户端,也可以创建内存中的用户。

TestUser类代表了一个测试用户和它的一些声明(claim)。我们现在在Config类中创建一些用户:

using IdentityServer4.Test;

public static List<TestUser> GetUsers()
{
return new List<TestUser>
{
new TestUser
{
SubjectId = "",
Username = "alice",
Password = "password"
},
new TestUser
{
SubjectId = "",
Username = "bob",
Password = "password"
}
};
}

然后在ConfigureService方法中注入:

public void ConfigureServices(IServiceCollection services)
{
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers());
}

AddTestUsers扩展方法在后台做了这么几件事:

  • 添加了对resource owner password这种授权类型的支持
  • 添加了通常在login ui中使用的用户相关的服务。
  • 在testuser上添加了profile service。

为resource owner password这种授权类型创建一个相应的客户端

如果你想要客户端对于这两种授权类型都支持,你可以在现有的客户端上面通过修改AllowedGrantTypes属性的值来添加对这种授权类型的支持。

通常情况下你只是想要创建一个单独的客户端作为resource owner password这种授权类型的场景下使用,在Config类的GetClients方法中添加以下的代码:

public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
// other clients omitted... // resource owner password grant client
new Client
{
ClientId = "ro.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}

使用password的授权类型来请求token

上面定义的那个客户端看起来和我们先前定义的client credentials客户端看起来很像。最主要的不同在于现在客户端会收集用户的密码,并在请求token的过程中将他连同其他东西一起发送到token service上面。

再次使用IdentityModel的TokenCLient来帮助我们实现这个请求:

// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice", "password", "api1"); if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
} Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");

当你向API发送token的时候,你会发现一个非常小而又非常重要的改变(相对于clientcredential这种授权类型来说):access token现在包含了一个”sub“的claim,这个claim就是用的唯一标识(在Config类中定义的TestUser的subjectid属性就是这里的东西),这个不同会通过api的方法返回的json里面发现。我在这里展示1下,我通过postman:

首先是通过client credential这种授权获取的token来访问api的,获取的结果如下:

[
{
"claimType": "nbf",
"claimValue": ""
},
{
"claimType": "exp",
"claimValue": ""
},
{
"claimType": "iss",
"claimValue": "http://localhost:5000"
},
{
"claimType": "aud",
"claimValue": "http://localhost:5000/resources"
},
{
"claimType": "aud",
"claimValue": "api1"
},
{
"claimType": "client_id",
"claimValue": "firstClient"
},
{
"claimType": "scope",
"claimValue": "api1"
}
]

看一看出没有sub这个声明。

然后使用resource owner password这个授权类型来搞到token,再用这个token访问一下api:

[
{
"claimType": "nbf",
"claimValue": ""
},
{
"claimType": "exp",
"claimValue": ""
},
{
"claimType": "iss",
"claimValue": "http://localhost:5000"
},
{
"claimType": "aud",
"claimValue": "http://localhost:5000/resources"
},
{
"claimType": "aud",
"claimValue": "api1"
},
{
"claimType": "client_id",
"claimValue": "secondClient"
},
{
"claimType": "sub",
"claimValue": ""
},
{
"claimType": "auth_time",
"claimValue": ""
},
{
"claimType": "idp",
"claimValue": "local"
},
{
"claimType": "scope",
"claimValue": "api1"
},
{
"claimType": "amr",
"claimValue": "pwd"
}
]

IdentityServer4【QuickStart】之使用ResourceOwnerPassword流程来保护API的更多相关文章

  1. 【IdentityServer4文档】- 使用客户端凭据保护 API

    使用客户端凭据保护 API quickstart 介绍了使用 IdentityServer 保护 API 的最基本场景. 接下来的场景,我们将定义一个 API 和一个想要访问它的客户端. 客户端将在 ...

  2. IdentityServer4【QuickStart】之使用ClientCredentials流程保护API

    使用ClientCredentials流程保护API 这个示例展示了使用IdentityServer中保护APIs的最基本的场景. 在这个场景中我们会定义一个API和一个想要访问它的客户端.客户端会在 ...

  3. IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API

    IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...

  4. IdentityServer4 中文文档 -9- (快速入门)使用客户端凭证保护API

    IdentityServer4 中文文档 -9- (快速入门)使用客户端凭证保护API 原文:http://docs.identityserver.io/en/release/quickstarts/ ...

  5. ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建API项目,配置IdentityServer保护API资源

    配套源码:https://gitee.com/jardeng/IdentitySolution 接上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(一):搭 ...

  6. asp.net core系列 54 IS4用客户端凭据保护API

    一. 概述 本篇开始进入IS4实战学习,从第一个示例开始,该示例是 “使用客户端凭据保护API”,这是使用IdentityServer保护api的最基本场景.该示例涉及到三个项目包括:Identity ...

  7. ASP.NET Core的身份认证框架IdentityServer4(7)- 使用客户端证书控制API访问

    前言 今天(2017-9-8,写于9.8,今天才发布)一口气连续把最后几篇IdentityServer4相关理论全部翻译完了,终于可以进入写代码的过程了,比较累.目前官方的文档和Demo以及一些相关组 ...

  8. 用ASP.NET Core 2.1 建立规范的 REST API -- 保护API和其它

    本文介绍如何保护API,无需看前边文章也能明白吧. 预备知识: http://www.cnblogs.com/cgzl/p/9010978.html http://www.cnblogs.com/cg ...

  9. Identity Server 4 - Hybrid Flow - 保护API资源

    这个系列文章介绍的是Identity Server 4 的 Hybrid Flow, 前两篇文章介绍了如何保护MVC客户端, 本文介绍如何保护API资源. 保护MVC客户端的文章: https://w ...

随机推荐

  1. 【2017山东day7】养猫

    [2017山东day7]养猫 Description 你养了一只猫,为了让它快乐地成长,你需要合理地安排它每天的作息时间.假设一天分为$ n \(个时刻,猫在每个时刻要么是吃东西,要么是睡觉.在第\) ...

  2. 采用BitMap从20亿个int正整数中找出相同的数字

    所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来存储数据,因此可以大大节省存储空间. public class Test { ...

  3. tomcat的webapps目录中的abc.war

    1  在tomcat的webapps目录中,会有类似abc.war类型的文件,在tomcat启动时会自动解压为abc目录. 2  如果tomcat在运行中,不要直接删除abc.war文件,删除后,ab ...

  4. java实现随机四则运算

    使用JAVA编程语言,独立完成一个包含3到5个数字的四则运算练习,软件基本功能要求如下: 程序可接收一个输入参数n,然后随机产生n道加减乘除练习题,每个数字在 0 和 100 之间,运算符在3个到5个 ...

  5. 【转】Windows中设置Fiddler抓HTTPS请求的解决办法 Unable to configure Windows to Trust the Fiddler Root certificate .

    官网设置 Click Tools > Fiddler Options > HTTPS. Click the Decrypt HTTPS Traffic box. 按照上述要求,我的设置是这 ...

  6. php 依赖注入的实现

    当A类需要依赖于B类,也就是说需要在A类中实例化B类的对象来使用时候,如果B类中的功能发生改变,也会导致A类中使用B类的地方也要跟着修改,导致A类与B类高耦合.这个时候解决方式是,A类应该去依赖B类的 ...

  7. 阅读ARM Memory(L1/L2/MMU)笔记

    <ARM Architecture Reference Manual ARMv8-A>里面有Memory层级框架图,从中可以看出L1.L2.DRAM.Disk.MMU之间的关系,以及他们在 ...

  8. face detection[Multi-view face detection&& MTCNN]

    因为这两篇论文感觉内容较短,故而合并到一个博文中. Multi-view face detection 本文来自<Multi-view Face Detection Using Deep Con ...

  9. Spring整合ActiveMQ及多个Queue消息监听的配置

        消息队列(MQ)越来越火,在java开发的项目也属于比较常见的技术,MQ的相关使用也成java开发人员必备的技能.笔者公司采用的MQ是ActiveMQ,且消息都是用的点对点的模式.本文记录了实 ...

  10. 基于 Django2 实现邮箱注册登录功能

    1. 开发环境 Python 3.6.5 Django 2.2 2. 项目功能 用户登录 邮箱注册 图形验证码 找回密码 修改密码 用户退出 3. 项目创建 首先创建项目: django-admin ...