Search code examples
c#asp.net.net-2.0forms-authentication

Error code for "not logged in" or page redirection


Is there an error code for handling users who are not logged in user but tries to enter a webpage by manually typing the URL?

Currently my web application redirects the user to the login page, but I would like to know if there is a better way to handle the page redirection to an error page in the web.config.

EDIT:

Forms tag in web.config:

<forms name=".ADAuthCookie" loginUrl="Login.aspx" defaultUrl="~/dataRefresh.aspx" timeout="20" />


Solution

  • You will need to in your Global.asax.cs, in the Application_EndRequest event handler

    You will need to create an HttpModule that handles the EndRequest event and does the following processing:

        if(Response.StatusCode == 401)
        {
        // add code to redirect to the access denied page
        }
    

    Your HttpModule will need to be registered in front of the UrlAuthorizationModule so that you can do the redirect before the UrlAuthorizationModule does its own.

    This will do this for all request that the forms authentication would have normally routed to the login page.