Role-based authorization for action without one for controller

I am trying to implement role-based authorization on a ASP.NET Core 8 Web API, and the goal is that users have access to nothing by default. Their role has to be declared explicitly.

Another requirement is that the Admin role should have access to absolutely everything.

I struggle to see how to implement this simple cybersecurity requirement because :

  1. If I don’t add the [Authorize] attribute on the controller, then it is completely open to anyone with any role. This default behavior seems extremely weird to me, as it means if we just forget to set the Authorize attribute, we completely open our sensitive API to any user.
  2. If I want to set a “default” role for the controller and then override it at the action level, I can’t because they are additive.

I have seen various answers here on SO to deal with this but usually they either answer part of the problem or they are completely outdated and relate to ASP.NET MVC, or they deal about making stuff just public/not public.

What I would like is this :

public class AppController : Controller
{
    [Authorize(Roles = "User")]
    public IActionResult ListItems() {} // <= Can be accessed by role "User"

    public IActionResult GetItem() {} // <= Can be accessed by no one
}

As an alternative, it would be acceptable for us to have this :

[Authorize("Admin")]
public class AppController : Controller
{
    [Authorize(Roles = "User")]
    public IActionResult ListItems() {} // <= Can be accessed by roles "User" OR "Admin"

    public IActionResult GetItem() {} // <= Can be accessed by "Admin"
}

But I have not seen any way to change the fact that if the role check for controller fails, it won’t reach the check for the action.

Inverting the logic (User on controller and then Admin where it’s due) is not OK because it brings us back to a lack of security, plus it still means that all admins need to also have the user role, which is a bit of overhead in role management.

I don’t know how to get to any of these two options, I have a feeling this implies re-implementing some custom things, but I don’t know which ones.

1

1-You can create a policy

Program.cs

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
});

YourController.cs

[Authorize(Policy = "AdminOnly")]
public class AdminController : ControllerBase
{
    public IActionResult GetAdminData()
    {
        return Ok("Admin data");
    }
}

2-You can give [AllowAnonymous] if all your endpoints requires auth this will be make the endpoint accessed by everyone.

[AllowAnonymous]
public class YourController: ControllerBase
{
    public IActionResult AnonymousEndpoint()
    {
        return Ok("This api is accessed by everyone");
    }
}

3-Or you can assign the roles into filter
[Authorize("Admin,User")]

New contributor

Barış Taner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

It should be

[Authorize(Roles="Admin,User")]
public class AppController : Controller
{
    
    public IActionResult ListItems() {} // <= Can be accessed by roles "User" OR "Admin"
    [Authorize(Roles = "Admin")]
    public IActionResult GetItem() {} // <= Can be accessed by "Admin"
}

You could read this document for more details

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật