如何在 c# webapi 中使用 IdentityModel.OidcClient 来验证和验证从支持 openid 连接的活动目录服务生成的令牌?

问题描述

我在 c# 中有一个 webapi 和一个前端 angular 应用程序。 angular 应用程序正在调用活动目录服务,例如 azure ad,以获取访问令牌。在随后的 webapi 调用中,web api 应用程序需要验证访问令牌。

到目前为止,我的 webapi 一直在使用 Microsoft.Owin.Security jwt 令牌验证。我想用 IdentityModel.Oidcclient 替换 Microsoft.Owin.Security (因为这是经过认证的 c# openid 连接库)。请帮助我了解如何使用 Oidcclient 框架来验证令牌。

解决方法

要验证令牌,您不应使用 IdentityModel.OidcClient,而应使用 Microsoft.AspNetCore.Authentication.JwtBearer Nuget 包在 API 中正确验证 JWT 令牌。