前面的部分:

Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始

Identity Server 4 从入门到落地(二)—— 理解授权码模式

Identity Server 4 从入门到落地(三)—— 创建Web客户端

Identity Server 4 从入门到落地(四)—— 创建Web Api

Identity Server 4 从入门到落地(五)—— 使用Ajax 访问 Web Api

Identity Server 4 从入门到落地(六)—— 简单的单页面客户端

认证服务和管理的github地址: https://github.com/zhenl/IDS4Admin

客户端及web api示例代码的github地址:https://github.com/zhenl/IDS4ClientDemo

前面我们试验的客户端都是有最终用户参与的,也就是需要用户登录进行认证。如果项目中存在后台服务访问Web Api,这种情况下没有用户参与认证过程,就需要使用Client Credentials flow。我们创建一个简单的控制台项目试验一下。

首先在认证服务的管理应用中创建一个新的客户端,选择使用Client Credentials flow:

然后设置作用域和客户端密钥,这里作用域中添加myapi,访问我们的测试Api:

在我们前面的测试解决方案中增加一个新的.Net 6控制台项目,名称为IDSClientConsole,创建完成后,引入程序包IdentityModel:



修改Program.cs如下:

using IdentityModel.Client;
using Newtonsoft.Json.Linq; await GetTokenAndCallApiAsync(); static async Task GetTokenAndCallApiAsync()
{
// discover endpoints from metadata
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync("http://localhost:4010");
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
} var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint, ClientId = "consoleclient",
ClientSecret = "secret1",
Scope = "myapi"
}); if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
} Console.WriteLine(tokenResponse.Json); // call api
var apiClient = new HttpClient();
apiClient.SetBearerToken(tokenResponse.AccessToken); var response = await apiClient.GetAsync("http://localhost:5153/WeatherForecast");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
else
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
}

设置解决方案的启动项目,将这个控台项目和Web Api设置为同时启动:



项目启动后,控制台应用通过认证,然后访问Web Api获取数据,结果如下:

到这里,我们已经试验了在Web应用、单页面应用和客户端应用中访问认证服务进行认证,还测试了认证服务对Web Api的保护,接下来准备落地,为在项目中实际使用做准备,还有如下问题需要解决:

  • 基于.Net Framework的遗留项目如何使用认证服务。
  • 现有的使用Asp.Net Core使用Identity的项目如何改造。
  • 简化客户端和Web Api的编程,将代码中写死的配置项移动到配置文件。
  • 认证服务和管理应用的多种部署方式:部署到IIS,部署到Docker容器等等。

接下来的部分我们将一一解决这些问题。

Identity Server 4 从入门到落地(七)—— 控制台客户端的更多相关文章

  1. Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  2. Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  3. Identity Server 4 从入门到落地(十)—— 编写可配置的客户端和Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  4. Identity Server 4 从入门到落地(十一)—— Docker部署

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  5. Identity Server 4 从入门到落地(十二)—— 使用Nginx集成认证服务

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  6. Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  7. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  8. Identity Server 4 从入门到落地(六)—— 简单的单页面客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  9. Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始

    最近项目中需要使用Identity Server 4,以前对这个技术只是有些了解,没有系统研究过,网上相关的资料不少,大多是从编写一个简单的认证服务开始,离能够落地使用有相当的距离,理论学习如何不结合 ...

随机推荐

  1. vector之erase和迭代器

    C++开发中使用vector时非常方便的,但是也是需要非常小心的,最近在使用容器删除某个元素的时候,测试结果出现了异常 /* vector erase test*/ int testVector() ...

  2. sum-root-to-leaf-numbers leetcode C++

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  3. Linux下向windows传输文件【sz 文件】没有弹框提示下载到什么位置

    Linux环境向windows环境传输文件 security crt工具,同同一个软件,连接不同服务器,有的服务器传送文件没有弹框选择要下载的文件路径,可以在[Options]-[Session Op ...

  4. 浅谈对typora的使用

    内容概要 - 什么是typora - typora的具体使用 目录 内容概要 - 什么是typora - typora的具体使用 1. 什么是typora 2.typora的具体使用 1.标题级别 2 ...

  5. exec系统调用 && 进程的加载过程

    exec系统调用会从指定的文件中读取并加载指令,并替代当前调用进程的指令.从某种程度上来说,这样相当于丢弃了调用进程的内存,并开始执行新加载的指令. exec系统调用会保留当前的文件描述符表单.所以任 ...

  6. C# StringBuilder和string

    StringBuilder和string 1.string是引用类型还是值类型 MSDN官方说string是引用类型: 引用类型:引用分配栈内存,引用类型本身的数据存储在堆中: 值类型:在函数中创建, ...

  7. CommonJS与ES6 Module的使用与区别

    CommonJS与ES6 Module的使用与区别 1. CommonJS 1.1 导出 1.2 导入 2. ES6 Module 2.1 导出 2.2 导入 3. CommonJS 与 ES6 Mo ...

  8. PTA 7-4 最小生成树的唯一性 (35分)

    PTA 7-4 最小生成树的唯一性 (35分) 给定一个带权无向图,如果是连通图,则至少存在一棵最小生成树,有时最小生成树并不唯一.本题就要求你计算最小生成树的总权重,并且判断其是否唯一. 输入格式: ...

  9. LeetCode -90. 子集 II C++ (回溯法)

    class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) ...

  10. Django笔记&教程 1-1 一 新建项目

    Django 自学笔记兼学习教程第1章第1节--一 新建项目 点击查看教程总目录 1- 命令行新建Django项目 新建项目命令(project_name处为项目名) django-admin sta ...