目的:创建IdentityServer 并通过PostMan验证获取token

第一次配置如下
 public class Config
{
public static IEnumerable<ApiResource> GetResource()
{
return new List<ApiResource>
{ new ApiResource("api","My Api") };
} public static IEnumerable<Client> GetClients()
{
return new List<Client> { new Client()
{
ClientId="client",
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={ new Secret("secret".Sha256())},
AllowedScopes={"api"}
} };
}
}

PostMan的请求如图

http://localhost:5000/connect/token

结果显示 请求无效 参照教程 >https://www.cnblogs.com/monster17/p/13261647.html 得知是因为传参格式的问题

  • 1.我们将Body改为 格式
  • 2.加参数<AllowedScopes: api> 【指定的api为配置Config中apiresource的名称】 再次请求

    如图

结果又显示scope无效 【这里懵逼了很久 明明Config中apiResource的name 和client中AllowedScopes的接口是一致的】

但是参照报错 一定是scop的问题 于是去找了一下官方文档关于API Resources 的使用

https://identityserver4.readthedocs.io/en/latest/topics/resources.html?highlight=apiresource#api-resources

参照原文



这里也便可以理解 问题所在了。 apiResource并不等同于scope 它相当于是scopes集合的统称 实现了scope的分组【但具体的scope还是要进行设置的 我们先前的Config 缺少具体scope =。= 真是令人头大】

先参照例子理解一下

//创建多个scope
{
return new List<ApiScope>
{
// invoice API specific scopes
new ApiScope(name: "invoice.read", displayName: "Reads your invoices."),
new ApiScope(name: "invoice.pay", displayName: "Pays your invoices."), // customer API specific scopes
new ApiScope(name: "customer.read", displayName: "Reads you customers information."),
new ApiScope(name: "customer.contact", displayName: "Allows contacting one of your customers."), // shared scope
new ApiScope(name: "manage", displayName: "Provides administrative access to invoice and customer data.")
};
} //调取时 将多个scope的Name加参时 可能会很长很长 为了方便 我们将scope 进行分组 后期可直接调用**统称** 这里分成两组
public static readonly IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("invoice", "Invoice API")//设置每组scopes的统称
{
Scopes = { "invoice.read", "invoice.pay", "manage" }//设置每组scope具体名称
}, new ApiResource("customer", "Customer API")
{
Scopes = { "customer.read", "customer.contact", "manage" }
}
};
}

再来回头看看我们的Config 修改如下

    public class Config
{ public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>() {
new ApiResource("api", "My api") { Scopes={"api1","api2"} //将具体的scopes 归为一组 统称为api
} };
}
//创建具体的scope
public static IEnumerable<ApiScope> GetApiScopes()
{
return new List<ApiScope> {
new ApiScope("api1","My first api"),
new ApiScope("api2","My second api")};
} public static IEnumerable<Client> GetClients()
{
return new List<Client>() { new Client {
ClientId="client",
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={new Secret("secret".Sha256()) },
AllowedScopes={ "api1","api2"}//此处一定要配置具体的scope名称 外部调用可直接配置为 api【统称】
} };
}

用PostMan验证一下

成功了 emmmm 又是美好的一天【还是要多读源文档e =.=】

补充 获取Refresh_token

GetClients配置修改如下

结果

.Net Core WebApi(三)--使用 IdentityServer4 4.1.1 踩坑记录的更多相关文章

  1. ASP.Net Core Web Api 使用 IdentityServer4 最新版 踩坑记录

    辅助工具 日志追踪包 : Serilog.AspNetCore 源码查看工具 : ILSpy 项目环境 ###: ASP.NetCore 3.1 IdentityServer4 4.0.0+ 主题内容 ...

  2. .net core建站踩坑记录

    系统:win10 VS版本:2017 .NET Core 版本: 1.1 零.读取配置文件 参考:http://www.tuicool.com/articles/QfYVBvi 此版本无需添加其他组件 ...

  3. Asp.Net Core Identity+EFCore + Mysql踩坑记录

    搭建基础框架准备试试传说中的Identity,本以为很顺利,结果一路踩了N多坑 遂就把过程记录下来.方便自己以后查看,也希望能帮到遇到同样问题的朋友. 1.首先,引入Identity需要的类库,还有M ...

  4. .NET CORE 2.0 踩坑记录之ConfigurationManager

    在之前.net framework 中使用的ConfigurationManager还是很方便的,不过在.NET CORE 2.0中SDK默认已经不存在ConfigurationManager. 那么 ...

  5. 关于.net core 在docker中监听地址设置踩坑记

    1.今天在做docker容器的时候发现如果将.net core 内部监听地址设置为localhost:8888. 2.在docker build -p 6444:8888 运行容器后,外部通过6444 ...

  6. .net Core发布至IIS完全手册带各种踩坑

    服务器环境配置 和各位大爷报告一下我的服务器环境 : Windows Server 2012 iis 8 小插曲开始: 运维大哥在昨天给了我一台新的server 0环境开始搭建 . 并且没有安装任何的 ...

  7. Kubernetes-Service介绍(三)-Ingress(含最新版安装踩坑实践)

    前言 本篇是Kubernetes第十篇,大家一定要把环境搭建起来,看是解决不了问题的,必须实战. Kubernetes系列文章: Kubernetes介绍 Kubernetes环境搭建 Kuberne ...

  8. 在 ASP.NET Core 中使用 MySql 踩坑记录

    使用 Pomelo.EntityFrameworkCore.MySql 生成 MySQL 数据库 关于如何使用请查看项目文档即可 组件地址:https://github.com/PomeloFound ...

  9. net core 踩坑记录

    静态文件要放到wwwroot目录中才能访问 linux服务器部署运行报错 System.Net.Http.HttpRequestException: The SSL connection could ...

随机推荐

  1. Mycat读写分离的简单实现

    目录 1.Mycat读写分离的配置 1.1.Mycat是什么 1.2.Mycat能干什么 1.2.1.数据库的读写分离 1.2.1.1.数据库读写分离图解 1.2.2.数据库分库分表 1.2.2.1. ...

  2. React组件三大属性之 props

    React组件三大属性之 props 理解1) 每个组件对象都会有props(properties的简写)属性2) 组件标签的所有属性都保存在props中 作用1) 通过标签属性从组件外向组件内传递变 ...

  3. 环境变量PATH还原方法

    修改/root/.bashrc 如果已经修改了,会出现什么问题?root用户shell找不到类似vi的命令,ls,export命令全部失效,如何去做? 以绝对路径的vi工具去删除错误的配置,然后退出再 ...

  4. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  5. odoo12常用的方法

    2019-09-13 今天是中秋节,星期五 #自定义显示名称 def name_get(self): result = [] for order in self: rec_name = "% ...

  6. Querydsl与SpringBoot集成

    Querydsl为大多数数据库提供了一种基于Java的类型安全,类SQL的查询方式.相比JPA,Querydsl能提供更加强大的查询方式,比如关联查询.相比MyBatis,Querydsl省去了XML ...

  7. 开机时自动启动的AutoHotkey脚本 2019年07月08日19时06分

    ;;; 开机时自动启动的AutoHotkey脚本;; 此脚本修改时间 2019年06月18日20时48分;; 计时器创建代码段 ------------------------------------ ...

  8. Python小白的数学建模课-10.微分方程边值问题

    小白往往听到微分方程就觉得害怕,其实数学建模中的微分方程模型不仅没那么复杂,而且很容易写出高水平的数模论文. 本文介绍微分方程模型边值问题的建模与求解,不涉及算法推导和编程,只探讨如何使用 Pytho ...

  9. 利用共享内存实现比NCCL更快的集合通信

    作者:曹彬 | 旷视 MegEngine 架构师 简介 从 2080Ti 这一代显卡开始,所有的民用游戏卡都取消了 P2P copy,导致训练速度显著的变慢.针对这种情况下的单机多卡训练,MegEng ...

  10. centos安装ansible

    此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 方法一. yum ins ...