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. Spring Boot RestApi 测试教程 Mock 的使用

    测试 Spring Boot Web 的时候,我们需要用到 MockMvc,即系统伪造一个 mvc 环境.本章主要编写一个基于 RESTful API 正删改查操作的测试用例.本章最终测试用例运行结果 ...

  2. 【原】Docker学习_Docker上传镜像至docker hub(4)

    构造镜像的两种方式:1.commit  2.Dockerfile Docker提供了一个docker commit命令,可以将容器的存储层保存下来成为镜像.换句话说,就是在原有镜像的基础上,再叠加上容 ...

  3. Android Studio如何更新support repository

    转自: http://blog.csdn.net/sinat_29696083/article/details/70256377 刚进新公司,熟悉新业务中.老大叫我看看关于ConstraintLayo ...

  4. Fleck WebSocket使用

    Fleck WebSocket使用 作为笔记存储. 最近公司有这方面的使用需求.在网上查了一些资料后.得到了想要的结果.以下记录摘抄至网上资料. 1.首先,服务端.项目NuGet直接引用Fleck类库 ...

  5. org.springframework.data.redis.RedisConnectionFailureException

    org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested ...

  6. IELTS Simon wr task p3

  7. linux用户权限、系统信息相关命令(待学)

    用户权限相关命令 目标 用户 和 权限 的基本概念 用户管理 终端命令 组管理 终端命令 修改权限 终端命令 01.用户和权限的基本概念 1.1 基本概念 用户 是Linux系统工作中重要的一环, 用 ...

  8. Uart学习笔记

    分享一个蛮好的链接:https://blog.csdn.net/wordwarwordwar/article/details/73662379 今天在看的资料是S家的DW_apb_uart的官方文档. ...

  9. Linux centosVMware Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl

    一.Nginx负载均衡 vim /usr/local/nginx/conf/vhost/load.conf // 写入如下内容 upstream qq_com { ip_hash; 同一个用户始终保持 ...

  10. 爬虫实战_爬取豆瓣图书利用csv库存储

    读取csv文件 通过csv.reader()和DictReader()两个函数 reader()函数返回一个迭代器 会包含表头 通过next函数可以跳过,但是它只能通过下标访问数据: DictRead ...