c# - dotnet core IdentityModel 不会内省从第三方 IDaaS 发出的令牌
问题描述
概述
我有一个 dotnet Core Web 服务(即 dotnet new webapi),我想用第三方 IDaaS 服务发布的参考令牌来保护它。我在 ConfigureServices 中添加了IdentityModel.AspNetCore.OAuth2Introspection库以及必要的附加代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
.AddOAuth2Introspection(options =>
{
options.ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx";
options.ClientSecret = "xxxxxxx";
options.IntrospectionEndpoint = "https://demo.verify.ibm.com/v1.0/endpoint/default/introspect";
});
}
无论我做什么,我都会不断收到来自 WebAPI 的 401 UNAUTHORIZED 响应。事实上,我什至没有看到 Web API 正在与 IDaaS 联系以验证承载令牌。
以下是 HTTP 跟踪:
GET /WeatherForecast HTTP/1.1
> Host: localhost:5001
> User-Agent: insomnia/2021.5.3
> Cookie: PD-S-SESSION-ID=1_2_1_K7PE6XKeEDKOkwioWpJxhxT8-Gdkz3TDgKXHgRIzMCKnQxYJ
> Authorization: Bearer **REMOVED**
> Accept: */*
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Date: Thu, 21 Oct 2021 17:15:14 GMT
< Server: Kestrel
< Content-Length: 0
* Connection #47 to host localhost left intact
针对以下端点:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
[Authorize]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
我想知道是否有人有做我想做的事情的经验?任何建议将不胜感激。
解决方案
问题是由于缺少app.UseAuthentication();
管道的中间件注册。
推荐阅读
- php - 如何在 php 环境中使用 Sql 查询逐行读取文本文件(UTF-8)并将数据放入 Sql 数据库文件
- vb.net - 将列值添加到一行
- geometry - 距离度量不同的单位
- visual-studio - 无法使用 Visual Studio 2019 和 Azure 发布 Web 应用程序
- python - 如何在 Python 中使用输入文件(bash 命令)运行二进制可执行文件?
- python - Pytest 如何在范围“类”中包含“设置”固定装置
- azure - Azure SignalR 服务连接字符串
- python - 如何根据另一个键的值从嵌套(不总是存在的)字典列表中提取给定键的值 [Python]
- java - 我在我的 JLabel 中添加了一个 gif,但它只播放一次并且无法继续,我该如何解决?
- regex - 聊天机器人问题 - 无限递归 - Codecademy 问题