views:

73

answers:

2

Hi,

I'm using the Authorize() attribute to secure my controllers/actions and want to only display the Login action to unauthenticated users - or to put it another way, deny access to authenticated users.

I haven't been able to find anything on the web dealing with either denying permission or allowing negative permissions (ie !LoggedIn)

Can someone please point me in the right direction?

MVC2, .Net 4

EDIT: To clairfy, I want something like this:

Public Class PublicController
    Inherits ControllerBase

    <Authorize()> 'Only logged-in users can logout
    Public Function Logout() as ActionResult
        Return View()
    End Function

    'Something here to indicate that only NON-authorized users should see this action
    Public Function Login() as ActionResult
        Return View()
    End Function

End Class
+1  A: 

I suggest you look at creating a custom ActionMethodSelectorAttribute. As described here: http://stackoverflow.com/q/2383094/148403

Clicktricity
+1  A: 

Could it be as simple as this:

public class DenyAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return !base.AuthorizeCore(httpContext);
    }
}
jfar
Interesting idea - The only problem is that I'm using the atributes to allow Telerik menu to securty trim my sitemap - I'm not sure if it will detect custom attributes (although I would hope so!). I'll give it a shot and get back to you in a day or so
Basiclife
@Basiclife - It will, I'm familiar with some of their stuff, should pick up custom attributes fine. - Also this is the sort of thing you should include in your question. What kind of components your hoping to integrate this with would change the answer.
jfar
@jfar Valid point :) and thanks
Basiclife