[Authorize]
public IActionResult SecretAction()
{
return Ok("This is a secret action!");
}
services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
});
[Authorize(Roles = "Admin")]
public IActionResult AdminAction()
{
return Ok("This is an Admin action!");
}
以上是一些常见的解决方法。但是,如果其他问题导致授权属性始终返回401错误代码,请进行更全面的故障排除,以解决问题。