I'm creating a custom role provider and I set a Authorize attribute specifying a role in my controller and it's working just fine, like this:
[Authorize(Roles="SuperAdmin")]
public class SuperAdminController : Controller
...
But when an user doens't have access to this controller, he's redirected to login page. How can I redirect him to a "AcessDenied.aspx" page?
Just a small update to Vic Alcazar, Added details of the request url in redirect So that can log the details of the access denied and by who if want
A slight improvement to Matt's answer by avoiding the need to hard-code the Logon page and optionally setting the access denied view within the attribute:
AccessDeniedAuthorizeAttribute.cs:
Take a look at tvanfosson's Answer from this very similar question, This is what I am doing(Thanks to tvanfosson), so now I just have to say:
If the user is not in the role, they will get thew view specified by ViewName.
Here's my solution, based on eu-ge-ne's answer. Mine correctly redirects the user to the Login page if they are not logged in, but to an Access Denied page if they are logged in but are unauthorized to view that page.
AccessDeniedAuthorizeAttribute.cs:
AccountController.cs:
Views/Account/Denied.cshtml: (Razor syntax)
Redirect is not always the best solution
Use standard http code 403: