1.基于概念

OAuth2.0与身份认证协议的角色映射

OpenID Connect 这个协议是2014颁发的,基于OAuth2.0,在这个协议中,ID Token会和Access Token一起发回客户端应用,它还提供了一个UserInfo这个端点,通过此端点可以获取用户信息,还提供了一级标识身份的scopes和claims(profile、email、address、phone)

这个协议定义了三个流程:

Identity Server4.0的结构图

2.三种流程模式

IdentityServer上:

在startup.cs页面中ConfiureServices页面中,应将json config 方式改为code config方式。即按如下方式切换注释代码

// in-memory, code config

builder.AddInMemoryIdentityResources(Config.GetIdentityResources());

builder.AddInMemoryApiResources(Config.GetApis());

builder.AddInMemoryClients(Config.GetClients());

// in-memory, json config

//builder.AddInMemoryIdentityResources(Configuration.GetSection("IdentityResources"));

//builder.AddInMemoryApiResources(Configuration.GetSection("ApiResources"));

//builder.AddInMemoryClients(Configuration.GetSection("clients"));

public static class Config

{

public static IEnumerable<IdentityResource> GetIdentityResources()

{

return new IdentityResource[]

{

new IdentityResources.OpenId(),

new IdentityResources.Profile(),

};

}

public static IEnumerable<ApiResource> GetApis()

{

return new ApiResource[]

{

new ApiResource("api1", "My API #1")

};

}

public static IEnumerable<Client> GetClients()

{

return new[]

{

// client credentials flow client

new Client

{

ClientId = "console client",

ClientName = "Client Credentials Client",

AllowedGrantTypes = GrantTypes.ClientCredentials,

ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) },

AllowedScopes = {"api1" }

}

};

}

}

客户端控制台程序代码:

static async Task Main(string[] args)

{

//Discovery endpoint

Console.WriteLine("Hello World!");

var client = new HttpClient();

var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");

if(disco.IsError)

{

Console.WriteLine(disco.Error);

return;

}

//Request access token,客户端必须带有:ClientCredentials

var tokenResponse =await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest {

Address = disco.TokenEndpoint,

ClientId = "console client",

ClientSecret = "511536EF-F270-4058-80CA-1C89C192F69A",

Scope= "api1"

});

if (tokenResponse.IsError)

{

Console.WriteLine(tokenResponse.Error);

return;

}

var apiClient = new HttpClient();

apiClient.SetBearerToken(tokenResponse.AccessToken);

var response = await apiClient.GetAsync("http://localhost:5002/api/values");

if (!response.IsSuccessStatusCode)

{

Console.WriteLine(response.StatusCode);

}

else

{

var content = await response.Content.ReadAsStringAsync();

Console.WriteLine(content);

}

asp.net core api资源应用:

public class Startup

{

public Startup(IConfiguration configuration)

{

Configuration = configuration;

}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

//使用IdentityServer认证和授权

services.AddMvcCore().AddAuthorization().AddJsonFormatters();

services.AddAuthentication("Bearer")

.AddJwtBearer("Bearer", options =>

{

options.Authority = "http://localhost:5000";

options.RequireHttpsMetadata = false;

options.Audience = "api1";

});

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{   //使用identityserver

app.UseAuthentication();

app.UseMvc();

}

}

[Route("api/[controller]")]

[Authorize]

[ApiController]

public class ValuesController : ControllerBase

{

// GET api/values

[HttpGet]

public ActionResult<IEnumerable<string>> Get()

{

return new string[] { "value1", "value2","value3"};

}

总结:Client Credentials这种方式,客户端应用不代表用户,客户端应用本身就相当于是资源所有者;通常用于机器对机器的通信;客户端也需要身份认证。

可采用工具软件监控客户端与服务端的通信:

将获取的access token放到网站https://jwt.io/,进行解码,即可以看到token中包含的许多用用信息。

IdentityServer4专题之五:OpenID Connect及其Client Credentials流程模式的更多相关文章

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

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

  2. IdentityServer4+OAuth2.0+OpenId Connect 详解

    一  Oauth 2.0 1 定义 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. ...

  3. MIT KIT OpenID Connect Demo Client

    Hello world! You are NOT currently logged in. This example application is configured with several pa ...

  4. IdentityServer4 实现 OpenID Connect 和 OAuth 2.0

    关于 OAuth 2.0 的相关内容,点击查看:ASP.NET WebApi OWIN 实现 OAuth 2.0 OpenID 是一个去中心化的网上身份认证系统.对于支持 OpenID 的网站,用户不 ...

  5. IdentityServer4 之Client Credentials走起来

    前言 API裸奔是绝对不允许滴,之前专门针对这块分享了jwt的解决方案(WebApi接口裸奔有风险):那如果是微服务,又怎么解决呢?每一个服务都加认证授权也可以解决问题,只是显得认证授权这块冗余,重复 ...

  6. .NET Core IdentityServer4实战 第二章-OpenID Connect添加用户认证

    内容:本文带大家使用IdentityServer4进行使用OpenID Connect添加用户认证 作者:zara(张子浩) 欢迎分享,但需在文章鲜明处留下原文地址. 在这一篇文章中我们希望使用Ope ...

  7. IdentityServer4【QuickStart】之利用OpenID Connect添加用户认证

    利用OpenID Connect添加用户认证 利用OpenID Connect添加用户认证 在这个示例中我们想要通过OpenID Connect协议将交互用户添加到我们的IdentityServer上 ...

  8. 基于 IdentityServer3 实现 OAuth 2.0 授权服务【客户端模式(Client Credentials Grant)】

    github:https://github.com/IdentityServer/IdentityServer3/ documentation:https://identityserver.githu ...

  9. OpenID Connect Core 1.0(一)介绍

    IdentityServer4是基于OpenID Connect and OAuth 2.0框架,OpenID Connect Core 1.0是IdentityServer4最重要的文档 By 道法 ...

随机推荐

  1. springboot @Select @Insert @Update @Delete

    https://blog.csdn.net/qq_20867981/article/details/80641353 使用@Select.@Insert.@Update.@Delete注解代替xxxM ...

  2. java 8 list的stream操作 list中的对象中的某一个成员取出转为该成员的list,以及对象过滤,筛选某个属性后的成员

    取成员属性list List<String> configList = codeEntityList.stream().map(t -> t.getName()).distinct( ...

  3. 于elasticsearch-rest-high-level-client 操作 Es

    安装Java:要求JDK为1.8及以上版本. 创建阿里云Elasticsearch实例:实例版本要求大于等于elasticsearch-rest-high-level-client的版本.本文创建一个 ...

  4. 解决PLSQL 查询后显示中文为问号(???)问题

    我的问题已解决,在装oracle的服务器上配置了下面的两个环境变量后,重启服务器,重新录入中文,在查询即可正确显示中文. 原因: 本机(装oracle的服务器)没有配置数据库字符集环境变量,或是与数据 ...

  5. HA: Infinity Stones-Write-up

    下载地址:点我 哔哩哔哩:点我 主题还是关于复仇者联盟的,这次是无限宝石的. 信息收集 虚拟机的IP为:192.168.116.137 ➜ ~ nmap -sn 192.168.116.1/24 St ...

  6. Thread的join方法

    一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程. Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行. 如下案列: 当NewT ...

  7. spark实验(一)--linux系统常见命令及其文件互传(2)

    2.使用 Linux 系统的常用命令 启动 Linux 虚拟机,进入 Linux 系统,通过查阅相关 Linux 书籍和网络资料,或者参考 本教程官网的“实验指南”的“Linux 系统常用命令”,完成 ...

  8. np.ndarray与PIL.Image对象相互转换

    Image对象有crop功能,也就是图像切割功能,但是使用opencv读取图像的时候,图像转换为了np.adarray类型,该类型无法使用crop功能,需要进行类型转换,所以使用下面的转换方式进行转换 ...

  9. Kubernetes集群部署及简单命令行操作

    三个阶段部署docker:https://www.cnblogs.com/rdchenxi/p/10381631.html 环境准备 [root@master ~]# hostnamectl set- ...

  10. 文件目录T位

    场景: 共享目录设置T标志 可以看别人的文件,但不可以删除.修改别人的文件 除非是ROOT,目录的拥有者