我正在尝试在 ASP.NET MVC Core 应用程序 (.NetCore 2) 上使用 IdentityServer4 实现身份验证和访问控制。虽然这不是我第一次实现后端,但这是第一次使用 .net,而且我正在为一些事情而苦苦挣扎。
我已按照 https://identityserver4.readthedocs.io/en/release/quickstarts/1_client_credentials.html 中的说明进行操作以及之前的页面。
我还添加了样本 IdentityController
正如他们所展示的:
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace leafserver.Controllers
{
[Route("/api/identity")]
[Authorize]
public class IdentityController : Controller
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
}
我的实现与他们的示例之间存在一些差异。据我所知:
ControllerBase
而不是 Controller
作为父类(super class) 我注意到的是以下内容:
[Authorize]
, 一切都很好。我得到 200 OK 的预期结果 [Authorize]
注释在那里,但我没有使用身份验证不记名 token ,我被重定向到登录页面(这不起作用,因为这是一个 web api,但这是以后的问题)[Authorize]
注释在那里,我使用(我认为是)正确的身份验证 token ,我收到 404 响应。 我原本希望得到 401 响应。
为什么我的路由不起作用,因为我使用的是身份验证 token ?
另外,我没有从服务器获取任何日志,这无济于事...
最佳答案
对我来说,答案是像这样在我的 Controller 上设置 Authorize 属性
[Authorize(AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]
这在最低权限指向的文档中进行了概述。
https://identityserver4.readthedocs.io/en/release/topics/add_apis.html
如果我只有 [Authorize] 那么它会产生 404
关于.net - 通过身份验证访问 [Authorize] Controller 时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49009977/