【问题标题】:dotnet core IdentityModel protect endpoints using scopesdotnet core IdentityModel 使用范围保护端点
【发布时间】:2021-12-09 07:21:18
【问题描述】:

概述

我有一个使用 OAuth2Introspection 保护的 dotNet Web API。授权由验证第三方发布的参考令牌的 Web API 确定。 Web API 有多个端点,其中一些需要不同的范围(例如读取与读写)。

问题

如何使用范围来控制对每个端点的访问?我希望能够执行以下操作:

using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class WeatherController : ControllerBase
    {
        private readonly ILogger<WeatherController> _logger;
        private readonly IConfiguration _config;

        public WeatherController(ILogger<WeatherController> logger, IConfiguration config)
        {
            _logger = logger;
            _config = config;
        }

        [HttpGet]
        [Authorize]
        [ScopeAuthorize("weather_read")]
        public ActionResult GetWeather()
        {
            return Ok();
        }

        [HttpPost]
        [Authorize]
        [ScopeAuthorize("weather_readwrite")]
        public ActionResult SetWeather(string weather)
        {
            return Ok();
        }
    }
}

【问题讨论】:

    标签: c# .net-core oauth-2.0


    【解决方案1】:

    这是通过定义一个或多个策略,然后将范围映射到来完成的。你的政策。然后,您使用 [Authorize(Policy = 'name')] 标记将策略分配给端点。详情请见this article

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2017-07-05
      • 1970-01-01
      • 2011-01-06
      • 2016-03-04
      相关资源
      最近更新 更多