惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

博客园 - 寻找无名的特质

开源轻量级工作流WorkflowCore介绍 C# 使用SpecFlow创建BDD测试用例 开源的.Net 工作流引擎Elsa初试——创建工作流服务器和图形化工作流配置管理应用 Asp.Net Core Identity 多数据库支持 创建NuGet本地包源 使用Visual Studio 2022开发前端 c# 一些警告的处理方法 Kendo UI Grid 批量编辑使用总结 Kendo UI Grid 使用总结 GoJS 使用笔记 Asp.Net Core: Swagger 与 Identity Server 4 VS Code开发TypeScript 使用Xamarin开发移动应用示例——数独游戏(八)使用MVVM实现完成游戏列表页面 使用Xamarin开发移动应用示例——数独游戏(七)添加新游戏 使用Xamarin开发移动应用示例——数独游戏(六)使用数据库 使用Xamarin开发移动应用示例——数独游戏(五)保存游戏进度 使用Xamarin开发移动应用示例——数独游戏(四)产生新游戏算法改进 使用Xamarin开发移动应用示例——数独游戏(三)添加回退和计时功能 使用Xamarin开发移动应用示例——数独游戏(二)创建游戏界面
使用PostMan Canary测试受Identity Server 4保护的Web Api
寻找无名的特质 · 2022-02-27 · via 博客园 - 寻找无名的特质

《Asp.Net Core: Swagger 与 Identity Server 4》一文中介绍了如何生成受保护的Web Api的Swagger文档,本文介绍使用PostMan Canary测试受Identity Server 4保护的Web Api。

首先搭建一下Identity Server 4的环境,并且创建一个测试用的Web Api和访问Web Api的客户端,这部分在系列文章《Identity Server 4 从入门到落地》中有详细的介绍。
Identity Server 4 Admin的代码和测试用Web Api和Client的代码可以从Github下载:
https://github.com/zhenl/IDS4Admin
https://github.com/zhenl/IDS4ClientDemo

这里只列出测试用Web Api项目appsettings.json中的配置项:

  "IdentityServer4Api": {
    "Authority": "http://host.docker.internal:4010",
    "CorsOrgins": [
      "http://host.docker.internal:5291"
    ],
    "Policies": [
      {
        "Name": "ApiScope",
        "RequireAuthenticatedUser": "true",
        "Claims": [
          {
            "ClaimType": "scope",
            "AllowValues": [ "testapi" ]
          }
        ]
      }
    ],

访问这个Web Api的测试用Web应用appsettings.json中相关配置项:

  "IdentityServer4Client": {
    "Authority": "http://host.docker.internal:4010",
    "ClientId": "testclient",
    "ClientSecret": "secret",
    "ResponseType": "code",
    "SaveTokens": "true",
    "RequireHttpsMetadata": "false",
    "Scopes": [ "openid", "profile", "testapi" ],
    "JsonKeys": []

  }

我们使用最新的PostMan Canary进行测试,下载地址 https://www.postman.com/downloads/canary/ 。安装完成后,就可以访问Web Api了。

如果直接访问 Web Api,会提示401错误:

我们需要在PostMan中实现认证,才能访问受保护的Web Api。我们已经在认证中心设置了可以访问Web Api的Client,信息如下:

Client ID testclient
Client Secret secret
Callback URL http://host.docker.internal:5291/signin-oidc
Web Api Scope Scope

现在,在PostMan中进入Authorization分页:

选择认证类型为OAuth2.0:

在右边出现认证需要的参数表单,其中Grant Type选择Authorization Code(With PKCE),说明我们使用OpenID Connect。其它参数参考已经配置的Client 参数。

还需要说明的是,认证的地址为:[认证服务地址]/connect/authorize,在这个例子中这个地址为 http://host.docker.internal:4010/connect/authorize 。获取Token的URL为[认证服务地址]/connect/token,在这个例子中地址为:http://host.docker.internal:4010/connect/token
参数填写完成后,点击Get Access Token按钮:

如果设置正确的话,会弹出认证页面,输入用户名,密码,认证完成后,PostMan关闭认证页面,并返回Access Token:

点击Use Token,Token被自动填写到访问页面:

这时再次按Send访问Web Api,可以正确地获取返回数据了: