Authorization
Control who can use the admin panel and each resource
By default, your admin panel is unsecured and anyone who can access your application can access the admin panel. To require authorization for the admin panel, call RequireAuthorization() in AddStellarAdmin. The example below only lets signed-in users with the Staff role use the admin panel:
builder.Services.AddStellarAdmin(admin =>
{
admin.RequireAuthorization(policy => policy.RequireRole("Staff"));
});RequireAuthorization() takes the same arguments as ASP.NET Core's RequireAuthorization() for endpoints:
| Argument | Who can use the admin panel |
|---|---|
| None | Any signed-in user |
Policy names, such as "Staff" | Users who meet the named policies, which you register with AddAuthorization() |
A delegate, such as policy => policy.RequireRole("Staff") | Users who meet the policy the delegate builds |
An AuthorizationPolicy | Users who meet the policy |
One or more IAuthorizeData, such as AuthorizeAttribute | Users who meet all of them |
If you call RequireAuthorization() more than once, users must meet every requirement. You can also call RequireAuthorization() on app.MapStellarAdmin() instead, which has the same effect.
StellarAdmin uses standard ASP.NET Core authorization, so your app must set up authentication and authorization as usual, including calling app.UseAuthorization().
When someone who isn't signed in opens the admin panel, ASP.NET Core asks them to sign in. When someone is signed in but doesn't meet the requirements, ASP.NET Core denies them access. With cookie authentication, both redirect to a page, which is /Account/Login or /Account/AccessDenied by default. Make sure your app has these pages, or set the cookie's LoginPath and AccessDeniedPath to your own pages.
Some parts of the admin panel's pages load after the page itself, such as row details that load when a row expands. When ASP.NET Core refuses one of these requests, for example because the user's session has expired, StellarAdmin reloads the whole page, so the user is redirected to the sign-in page or denied access as usual.
StellarAdmin handles these refused requests by wrapping your app's IAuthorizationMiddlewareResultHandler. If your app registers its own IAuthorizationMiddlewareResultHandler, register it before calling AddStellarAdmin(). If you register it afterwards, it replaces StellarAdmin's, and a refused request displays the sign-in page inside the part of the page that was loading.
In the Development environment, the setup checklist on the admin panel's home page shows whether the admin panel requires authorization.
Requiring authorization for a resource
To require authorization for a single resource, call RequireAuthorization() on the resource. It takes the same arguments as the admin panel's RequireAuthorization(), and users must meet both. The example below only lets users with the Finance role use the Suppliers resource:
builder.Services.AddStellarAdmin(admin =>
{
admin.RequireAuthorization(policy => policy.RequireRole("Staff"));
admin.AddEfCoreResource<Supplier>(resource =>
{
resource.RequireAuthorization(policy => policy.RequireRole("Finance"));
});
});The requirement covers every page of the resource. StellarAdmin also hides the resource from the sidebar and the home page for users who don't meet it.
A user who can't use the Suppliers resource can still select a supplier in a lookup on another resource, such as an accommodation's supplier. StellarAdmin hides the lookup's New button from them, since it opens the Suppliers resource's create page.
What each setting protects
Only the admin panel's and the resources' requirements stop people from opening pages. The other settings only hide items from the sidebar:
| Setting | Stops people from opening | Hides from the sidebar |
|---|---|---|
admin.RequireAuthorization() | Every page of the admin panel | |
resource.RequireAuthorization() | Every page of the resource | The resource |
link.RequireAuthorization() on a sidebar link | Nothing | The link |
item.Visible = false on a resource's sidebar item | Nothing | The resource |
[Authorize] on your own page or controller | That page, or the controller's pages |
A sidebar link points to one of your own pages, so StellarAdmin can't protect that page. Add [Authorize] to the page or its controller.
What StellarAdmin doesn't protect
StellarAdmin can't require authorization for a single action of a resource. For example, you can't let everyone view a resource and only let managers delete. To do that, leave the action off the resource, or check the user in a custom handler and return ResourceOperationResult.ValidationFailed(). To get the user in the handler, inject IHttpContextAccessor.
StellarAdmin also can't restrict which records a user can access. Scopes only filter the index page, and anyone can select any scope by changing the URL. To hide records from some users, use an EF Core global query filter or a custom data source.
Signing out
When someone is signed in, the sidebar displays the user menu, which has a Log out menu item.