IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可)

参考

官方文档:2_resource_owner_passwords

概念:资源拥有者密码凭据许可

认证服务端配置

认证服务ApiResource配置

new ApiResource("api1", "api项目 一")
{
ApiSecrets = { new Secret("api1pwd".Sha256()) }
},

认证服务Client配置

//resource owner password grant client
new Client
{
ClientId = "ro.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
AccessTokenType = AccessTokenType.Reference, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
},

认证服务Startup配置

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());
}

配置完成启动访问http://localhost:5000/.well-known/openid-configuration

资源服务Api配置

资源服务器Startup配置

services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters(); services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false; options.ApiName = "api1";
options.ApiSecret = "api1pwd"; //对应ApiResources中的密钥
});

添加接口

[Route("[controller]")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var info = from c in User.Claims select new { c.Type, c.Value };
var list = info.ToList();
list.Add(new { Type = "api1返回", Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
return new JsonResult(list);
}
}

Client客户端

(A)资源拥有者提供给客户端它的用户名和密码。
(B)通过包含从资源拥有者处接收到的凭据,客户端从授权服务器的令牌端点请求访问令牌。当发起请求时,客户端与授权服务器进行身份验证。
(C)授权服务器对客户端进行身份验证,验证资源拥有者的凭证,如果有效,颁发访问令牌。

//resource owner password grant client
private void btnROAuth_Click(object sender, EventArgs e)
{
Task<DiscoveryResponse> discoTask = DiscoveryClient.GetAsync(txtROAuthSer.Text);
discoTask.Wait();
var disco = discoTask.Result;
if (disco.IsError)
{
MessageBox.Show(disco.Error, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtROResult.Text = string.Empty;
txtAccessToken.Text = string.Empty;
return;
} // request token
var tokenClient = new TokenClient(disco.TokenEndpoint, txtROClient.Text, txtROSecret.Text);
Task<TokenResponse> tokenTask = tokenClient.RequestResourceOwnerPasswordAsync(txtROUser.Text, txtROPwd.Text, txtROScopes.Text);
tokenTask.Wait();
var tokenResponse = tokenTask.Result; if (tokenResponse.IsError)
{
MessageBox.Show(tokenResponse.Error, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtROResult.Text = string.Empty;
txtAccessToken.Text = string.Empty;
return;
} txtROResult.Text = tokenResponse.Json.ToString();
txtAccessToken.Text = tokenResponse.AccessToken;
}

调用Api

private void btnCallApi_Click(object sender, EventArgs e)
{
// call api
var client = new HttpClient();
client.SetBearerToken(txtAccessToken.Text); var responseTask = client.GetAsync(txtCCApiUrl.Text);
responseTask.Wait();
var response = responseTask.Result;
if (!response.IsSuccessStatusCode)
{
MessageBox.Show(response.StatusCode.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtApiResult.Text = string.Empty;
}
else
{
var contentTask = response.Content.ReadAsStringAsync();
contentTask.Wait();
var content = contentTask.Result;
txtApiResult.Text = JArray.Parse(content).ToString();
}
}

获取token过程解析

Jwt形式获取access_token、客户端身份验证两种方式,参考上一篇获取token过程解析。

Reference形式获取access_token

将client的AccessTokenType设置为1

再次获取的access_token不包含Claim信息。

此时获取的access_token(加密后)对应PersistedGrants表中的key

调用Api资源服务过程解析

调用过程与上一篇调用Api资源服务过程解析一样。

IdentityServer4之Resource Owner Password Credentials(资源拥有者密码凭据许可)的更多相关文章

  1. IdentityServer4 之 Resource Owner Password Credentials 其实有点尴尬

    前言 接着IdentityServer4的授权模式继续聊,这篇来说说 Resource Owner Password Credentials授权模式,这种模式在实际应用场景中使用的并不多,只怪其太开放 ...

  2. asp.net core IdentityServer4 实现 resource owner password credentials(密码凭证)

    前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...

  3. OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)

    授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...

  4. 不要使用Resource Owner Password Credentials

    不要使用Resource Owner Password Credentials 文章链接在这里 前言 最近公司项目在做一些重构,因为公司多个业务系统各自实现了一套登录逻辑,比较混乱.所以,现在需要做一 ...

  5. 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】

    适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...

  6. 使用Resource Owner Password Credentials Grant授权发放Token

    对应的应用场景是:为自家的网站开发手机 App(非第三方 App),只需用户在 App 上登录,无需用户对 App 所能访问的数据进行授权. 客户端获取Token: public string Get ...

  7. 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】

    密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码.客户端使用这些信息,向"服务商提供商"索要授权 ...

  8. IdentityServer4专题之六:Resource Owner Password Credentials

    实现代码: (1)IdentityServer4授权服务器代码: public static class Config {  public static IEnumerable<Identity ...

  9. ABP中使用OAuth2(Resource Owner Password Credentials Grant模式)

    ABP目前的认证方式有两种,一种是基于Cookie的登录认证,一种是基于token的登录认证.使用Cookie的认证方式一般在PC端用得比较多,使用token的认证方式一般在移动端用得比较多.ABP自 ...

随机推荐

  1. Amazon新一代云端关系数据库Aurora

    在2017年5月芝加哥举办的世界顶级数据库会议SIGMOD/PODS上,作为全球最大的公有云服务提供商,Amazon首次系统的总结了新一代云端关系数据库Aurora的设计实现.Aurora是Amazo ...

  2. 理解ClassLoader

    --摘自<Android进阶解密> *Java中的ClassLoader* 1.系统类加载器包括3种: 1)Bootstrap ClassLoader(引导类加载器) C/C++代码实现的 ...

  3. Alpha冲刺(1/10)——2019.4.23

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...

  4. Codeforces 446A. DZY Loves Sequences (线性DP)

    <题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...

  5. WinCC OA基本概念

    WinCC OA 是一个模块化软件架构的系统.所需的功能由不同任务创建的特定单元处理.在WinCC OA中,这些单元称为管理器 - 管理器是软件自身的一些独立的处理过程. 图:WinCC OA系统由功 ...

  6. [python]numpy.mean()用法

    a=np.array([[[1,1],[2,2],[3,3]],[[4,4],[5,5],[6,6]],[[7,7],[8,8],[9,9]],[[10,10],[11,11],[12,12]]]) ...

  7. 字符串转义为HTML

    有时候后台返回的数据中有字符串,并需要将字符串转化为HTML,下面封装了一个方法,如下 // html转义 function htmlspecialchars_decode(string, quote ...

  8. 二、JAVA基础、语法

    第二节:JAVA基础.语法 1.修饰符.变量:    Java中主要有如下几种类型的变量    局部变量                                                 ...

  9. rest_framework之权限源码剖析

    权限问题 1.models.py 2.用户类型: 3.views.py: 假设订单相关业务(只有SVIP用户有权限) 假设用户信息相关业务(只有普通用户.VIP有权限) 4.运行结果: 基本使用 以上 ...

  10. 32、可以拿来用的JavaScript实用功能代码

    可以拿来用的JavaScript实用功能代码(可能会有些bug,用时稍微修改下,我用了几个还可以) 转载自 1.原生JavaScript实现字符串长度截取 function cutstr(str, l ...