I use CookieAuthentication and Policy to check users and redirect them to the Account / Forbidden URL when it fails ... On the page I want to display some details, for example
- The page they were trying to access.
- The reason for the failure.
- And other debugging information ...
There are 6 types of user levels in my application. At the moment, it redirects users to the "Prohibition" page when it does not respond to UserRole ... but I could not trace which page they tried to access before Forbidden occurred ...
ConfigureServices () in Startup.cs
services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
});
Configure () in Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
My controller:
[Authorize(Policy = "AdminOnly")]
public class MyController : Controller
{
....
}