ASP.NET Security – Custom Error Handling

Any application we built, will not be completely error free. Application will throw an exception at one or other point. Its a good practice to handle such exception in a way that it wont expose any critical information that would invite hackers.

When an error occurred in application if its not handled properly, it will show up many critical information like Physical path, Framework version number, internal implementation(code) and other information. (Refer below image)

Image1

This can be avoided by enabling in Web.config, which will avoid exposing such critical information to public.

Step 1:

Enable “customErrors”, add section under <system.web> section and set its mode to “ON”.

Default mode of will be “OFF” – This will expose error details to user on web page

Mode “ON” – This will hide information getting exposed to user on web page.

Mode “Remote Only” - This is required when you are working on development machine, when running locally it throws exception details on web page which helps  in debugging, if request comes from remote machine it treats such request as customError mode ON. i.e., hinds critical information.

Image2 

Step 2:

For better user experience, we can redirect user to customError page, for that set “defaultRedirect” property in tag, which redirects to specified page when exception is thrown.

However, when redirection happens it changes URL too, which somewhat looks like (Example URL): , where “/Contact” specifies that error occurred in Contact page and then navigated to “Error.aspx” page, which is not necessary to be displayed for user. This can be avoided with a configuration under tag, need to set “redirectMode” to “ResponseRewrite”. Then tag looks like below,

With this setting URL will not be changed, looks like (Example URL)  

With all these settings, we can avoid exposing any internal code or any such information to user and application is prepared to handle exceptions thrown at runtime.

Hope this helps.

Thanks!

This article is part of the GWB Archives. Original Author: Manjunath K

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.