26 December 2011

Redirect to custom 401 page (invalid credentials)

Properly setting up a custom 401 redirect page in SharePoint 2010


This article will get you going with setting up a custom 401 redirect page in SharePoint 2010.

Note: this method is not confined to 401 errors only.. it can be any other type of errors at IIS level
Note 2: this is also not confined to SharePoint.. it can be very well applied to any ASP.Net web applications

Configuring Anonymous site for invalid credentials

Enable Anonymous Access for Web Application

1) Open the Central Admin.
2) Click Application Management > Manage web applications > [[target webapp]] > Authenticated Providers

3) Select the zone in which anonymous access is to be granted
4) Check the Enable anonymous access checkbox and save


Create Anonymous Access Site for the Web Application

1) Open the site collection where anonymous site is to be created for the Web-App
2) Create a subsite named "help" or whatever..
3) Click Site Actions > Site Permissions > Anonymous Access
4) Select Entire web site and click on Save



Setting up IIS to handle custom 401 page

Create a default redirect file

1) Create a new html file.. Default.htm and place it in “C:\inetpub\custerr\en-US\Custom”
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Authentication Need Help ?</title>
  <style type="text/css">
   html,
   body {
    height: 100%;
    padding: 0;
    margin: 0;
    overflow: hidden;
   }
  </style>
  <script type="text/javascript">
  window.location = "http://SP2010:2000/help/default.aspx?IsDlg=1";
  </script>
 </head>
 <body oncontextmenu="return false;">
  
 </body>
</html>
2) Edit Default.htm > set the “window.location” value to the url of the anonymous site created above.
3) Save and Exit file edit


Modify applicationHost.config to enable custom error on target Web-Application

1) Open file “C:\Windows\System32\inetsrv\config\applicationHost.config” in notepad.
2) Find the tag (replace with valid webapplication Name as in IIS manager)
<location path="xxxxxxxxxxxxxxxxxx">
3) Replace
<httperrors existingresponse="PassThrough"> 
with
<httperrors allowabsolutepathswhendelegated="true" errormode="Custom" existingresponse="PassThrough" lockattributes="allowAbsolutePathsWhenDelegated,defaultPath">
4) Save and Exit


IIS entry for the redirect page in target Web-Application

1) Open IIS Manager (Start > run > inetmgr)
2) Select the Web Application > Error Pages
3) Add a custom error page for 401.1 and point it to the custom Default.htm
4) Similarly, repeat step 3 to make an entry for 401.2
5) Also, Edit the default 401 item to set the same path and Exit IIS Mgr
6) Edit the web.Config of the webApp and replace
<httperrors> 
with
<httperrors errormode="Custom" existingresponse="Auto"> 
7) Save and close.


ref:
http://learn.iis.net/page.aspx/156/understanding-iis-configuration-delegation/
http://stackoverflow.com/questions/2472818/500-19-error-in-iis7-when-an-error-occurs


programmatically manipulate applicationhost.config
http://learn.iis.net/page.aspx/242/extending-iis-schema-and-accessing-the-custom-sections-using-mwa/
http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/
http://msdn.microsoft.com/en-us/library/microsoft.web.administration.aspx