Using Fluent Security, I have configured website access using DenyAnonymousAccess, DenyAuthenticationAccess and RequireRole.
SecurityConfigurator.Configure(configuration =>
{
configuration.ResolveServicesUsing(new FluentSecurityServiceLocator());
configuration.GetAuthenticationStatusFrom(CurrentUser.IsAuthenticated);
configuration.GetRolesFrom(CurrentUser.Roles);
configuration.For<HomeController>().DenyAnonymousAccess();
configuration.For<ReportsController>().RequireRole(UserRole.Administrator);
configuration.For<AccountController>().DenyAuthenticatedAccess();
configuration.For<AccountController>(x => x.ChangePassword()).DenyAnonymousAccess();
});
I've handled the PolictyViolationException for DenyAnonymousAccess and redirected to the logon page.
public ActionResult Handle(PolicyViolationException exception)
{
return new RedirectToRouteResult(
new RouteValueDictionary(new { action = "Login", controller = "Account" })
);
}
But I'm not sure if catching an exception from RequireRole is the same process? I need to redirect if RequireRole is violated.
Also, when user isn't logged on and clicks a link attached to a role, I get the unhandled version of denyanonymousaccess exception. What am I doing wrong in my configuration and implementation??