views:

299

answers:

2

Hi,

is there any generic way to get the role which is required for some particular action?

In Detail my problem is, that I have e.g. 2 roles "User" and "Admin" and an action with the following:

[Authorize(Roles = "Admin")] public class AdministrationController...

If the user is not logged in, he gets the login screen. That's ok. When a user with the role "Admin" logs in, all works fine. But if a user with the role "user" logs in, he will be continiously displayed the login screen, besides he has succesfully logged in, but of course not the required role.

So it would be good if the login display could display to him a hint, telling that he is already logged in, but he has not the required rights to see this page. And additionally it may display the required role(s).

Thanks, Michael

+1  A: 

You can write a custom IActionFilter attribute.

See this post: MVC – Custom Action Filters

Or Override Authorize Attribute in ASP.NET MVC

CD
+1  A: 

I think this mostly stems from the default AuthorizeAttribute design of showing the login page if any part of Authorization fails.

You may want to create a new class that inherits from AuthorizeAttribute. You can then override the default behaviour and show a different page. See http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/#comment-159 for a great example of this.

eyesnz