views:

21

answers:

1

I have admin area of my site: http://www.mysite.com/webadmin and I want to protect it by role (I'm using ASP.NET forms auth), so that only a user with the role "admin" could access it. In web.config I added this entry:

<location path="WebAdmin">
    <system.web>
        <authorization>
            <deny users="*"/>
            <allow roles="admin"/>
        </authorization>
    </system.web>
</location>

and it "sort of" works - it redirects you to the login page if you are not in role "admin". But I don't want that, I want to show an error page instead. Any way I could control that behavior?

Thank you, Andrey

+1  A: 

You can change the URL your file thinks is the login page via web.config. (See http://www.15seconds.com/issue/020220.htm.)

Consider substituting your custom error page url for the real login URL in configuration.

Edit:

The web.config approach is viable if implementing this as a general solution throughout an entire virtual directory. (Attempting to configure custom loginUrl's under a <location> element in web.config will result in a configuration error.)

You can have finer-grained control of this behavior imperatively by injecting code such as this in your admin page (or a base class for your admin pages):

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    if (!User.IsInRole("admin"))
    {
        Response.Redirect("~/ErrorPage.aspx?reason=denied");
    }
}

You may also look into creating an HttpModule, or tapping into your Global.asax, to handle authorization in a more general way without relying on page inheritance. See http://msdn.microsoft.com/en-us/library/ms227673.aspx. Use the BeginRequest event to inspect the URL path, and if it matches your pattern, deliver the error or redirect you want to deliver.

kbrimington
My app is using forms auth for authentication/authorization all over the place, so it needs to know the url to the real login page, so I can't change login page url in web.config ...unless I misunderstood what you meant...
Andrey
@Andrey: The redirect behavior when a user is not authenticated is driven from this web.config value. You can still place hyperlinks on your pages to the real login page; this approach will simply fool the redirecting engine to send users to your error page.
kbrimington
@Andrey: It's not a bad approach, despite the downvote. I've used it before to good effect.
kbrimington
OK, maybe I am misunderstanding what you suggest - you are saying I should change loginURL parameter in web.config to my error page? Then how woudl unauthenticated user get redirected to the real login page when they are trying to access pages that required authentication? Can you give an example?
Andrey
Perhaps it is I who misunderstood. I had thought you wanted the error page as a general solution to handling users accessing pages that require authentication. You want to have the error behavior only for authenticated pages in the location described?
kbrimington
Yes, thats correct :) I want the whole site work as usual with standard forms auth, but only the WebAdmin folder and down to show an error page if user doesn't have permissions
Andrey
@Andrey: Duly noted. I updated the post with additional options to handle your specific scenario imperatively: adding code to the page or page base class, adding an `HttpModule`, or adding code to your Global.asax.
kbrimington