-
Notifications
You must be signed in to change notification settings - Fork 0
Authentication and Secure Pages
Imp integrates with host-provided ASP.NET Core authentication. It does not authenticate users itself.
Register an actual authentication scheme and place authentication/authorization middleware before Imp:
builder.Services
.AddAuthentication(/* defaults */)
.AddCookie(/* options */);
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseImp(/* configuration */);Add [SecurePage] to a page:
using Imp.TemplateManagers;
[SecurePage]
public sealed class Account : BasePage { }Then configure the callback:
.Authenticate((context, page) =>
{
if (context.User.Identity?.IsAuthenticated == true)
return true;
context.Response.Redirect("/login");
return false;
})Returning false stops pre-render, postback, and page rendering. The callback must set an appropriate response—redirect, 401, or 403.
Important: [SecurePage] alone does nothing when no Authenticate callback is configured. Treat configuring both pieces as one security requirement and cover it with integration tests.
The callback can inspect the page type, claims, roles, policies, route, and request. Authentication only establishes identity; pages that require ownership or a particular permission still need authorization.
For complicated policy evaluation, resolve an authorization service before Imp or design an application service used by the callback. Avoid scattering claim-string comparisons through page rendering methods.
Authentication does not replace antiforgery protection. A secure state-changing page should require:
- an authenticated identity;
- authorization for the action/record;
- a valid antiforgery token;
- server-side input validation.
Never rely on hidden fields for ownership. Reload the target under the authenticated user before changing it.
Validate return URLs as local before redirecting after login. Do not send users to arbitrary query-string URLs. For API-like responses, prefer 401/403 over an HTML login redirect.
Imp is MIT licensed. See the source and Todo sample on GitHub.
Imp
Templates
Application integration
- Forms and antiforgery
- Dependency injection
- Authentication and secure pages
- Static assets and CDN paths
- Configuration reference
Help and reference