首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dotnet核心IdentityModel将不自省第三方IDaaS发行的令牌。

dotnet核心IdentityModel将不自省第三方IDaaS发行的令牌。
EN

Stack Overflow用户
提问于 2021-10-21 17:19:21
回答 1查看 172关注 0票数 2

概述

我有一个dotnet核心web服务(即dotnet新的webapi),我想用第三方IDaaS服务发出的引用令牌来保护它。我添加了IdentityModel.AspNetCore.OAuth2Introspection库以及ConfigureServices中必需的附加代码:

代码语言:javascript
复制
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个未经授权的回应。事实上,我甚至没有看到web正在扩展到IDaaS来验证Bearer令牌。

下面是HTTP跟踪:

代码语言:javascript
复制
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

针对以下端点:

代码语言:javascript
复制
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();
        }
    }
}

我想知道是否有人有做我想做的事的经验?如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-22 20:56:11

问题是由于缺少管道的app.UseAuthentication();中间件注册。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69666238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档