Display Login page when 401 error in ASP.Net

sds
 
on Dec 17, 2021 01:21 AM
435 Views

Hi

For 401 Error, When I place /Login.htm in 'Execute a URL on this site' session of IIS.

Login page is not showing when the 401 error hits. (401 error is happening when user click cancel in login form with out entering the login details).

I could see in IIS Authentication Windows Authetication with Status 'Enabled' and Response Type 'HTTP 401 Challenge'.

How can i make it work , so that when 401 happened it will show the login.htm

Or please inform is this possible to do.

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 17, 2021 01:26 AM
on Jan 04, 2022 11:15 PM

 

Hi sds,

Use Application_EndRequest and check the Response.Status in Global.asax file.

Code

protected void Application_EndRequest(Object sender, EventArgs e)
{
   HttpContext context = HttpContext.Current;
   if (context.Response.Status.Substring(0,3).Equals("401"))
   {
      context.Response.ClearContent();
      context.Response.Write("<script>"self.location='../login.htm';</script>");
   }
}

 

 

sds
 
on Dec 17, 2021 02:10 AM

I am using web form (vs 2015)

please inform ,where should i place the above code .

dharmendr
 
on Dec 17, 2021 02:28 AM

You need to add the code in Global.asax file.