Skip to main content

Attributes and Minimal APIs

URL rules managed in the panel are enough for most pages. Use the attributes when a check belongs in code, for example a permission that a page needs on top of its URL rule.

Attributes (MVC, Razor Pages)​

using SuperAdmin.Rbac.Authorization;

[HasPermission("posts.publish")] // user must hold the slug (on top of the URL rule)
public IActionResult Publish(int id) { ... }

[AllowRoles("editor", "author")] // member of at least one role (super admins pass)
public class DraftsController : Controller { ... }

[DenyRoles("guest")] // members of these roles are refused
public IActionResult Export() { ... }

[SkipUrlPermission] // any signed-in admin user, no URL rule needed
public IActionResult Help() { ... }

[AllowAnonymous] // public even inside /admin
public IActionResult Status() { ... }
AttributeEffect
[HasPermission("slug")]The user must hold the permission slug. The URL rule is still checked
[AllowRoles("a", "b")]The user must be in at least one of the roles. Super admins pass
[DenyRoles("a")]Members of these roles are refused
[SkipUrlPermission]Any signed-in admin user may open it. No URL rule is needed
[HasUrlPermission]Checks the permission URL rules through the authorization system. Same as [Authorize(Policy = SuperAdminDefaults.UrlPolicy)]. Needed for Blazor pages, see Blazor Server
[AllowAnonymous]Public, even inside the protected scope
note

These attributes need app.UseAuthorization() after app.UseSuperAdmin().

Minimal APIs​

app.MapPost("/admin/api/posts/{id:int}/publish", Publish).RequirePermission("posts.publish");
app.MapGet("/admin/api/reports", Reports).AllowRoles("manager");
app.MapGet("/admin/api/ping", () => "pong").SkipUrlPermission();

Paths that match /admin/api/* (the default Protection:ApiPaths) get 401 / 403 status codes instead of redirects.