Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace surekam. SPM. Portal. Website
{
Public partial class accessdenied: system. Web. UI. Page
{
Private int _ authenticationattempts = 0;
Public int authenticationattempts
{
Get
{
If (! String. isnullorempty (string. Format ("{0}", session ["authenticationattempts"])
{
Int. tryparse (session ["authenticationattempts"]. tostring (), Out _ authenticationattempts );
}
Return _ authenticationattempts;
}
Set
{
_ Authenticationattempts = value;
Session ["authenticationattempts"] = _ authenticationattempts;
}
}
Private string _ currentuser = string. empty;
Public String currentuser
{
Get
{
_ Currentuser = request. logonuseridentity. Name;
Session ["currentuser"] = _ currentuser;
Return _ currentuser;
}
Set
{
_ Currentuser = value;
Session ["currentuser"] = _ currentuser;
}
}
Private string _ previususer = string. empty;
Public String previususer
{
Get
{
_ Previususer = string. Format ("{0}", session ["previususer"]);
Return _ previususer;
}
Set
{
_ Previususer = value;
Session ["previususer"] = _ previususer;
}
}
/// <Summary>
/// This event fires on every page Load
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void page_load (Object sender, eventargs E)
{
// Make sure the browser does not cache this page
This. disablepagecaching ();
// Increase authentication attempts
This. authenticationattempts = This. authenticationattempts + 1;
If (this. authenticationattempts = 1)
{
// Change previous user to current user
This. previususer = This. currentuser;
// Send the first 401 response
This. send401 ();
}
Else
{
// When a browser is set to "automaticaly sign in with current credentials", we have to send two 401 responses to let the browser re-authenticate itself.
// I don't know how to determine if a browser is set to "automaticaly sign in with current credentials", so two 401 responses are always send when the user
// Does not switch accounts. In micrososft Office Sharepoint the user has to supply the credentials 3 times, when the user does not switch accounts,
// So it think this is not a problem.
If (this. authenticationattempts = 2 & this. currentuser. Equals (this. previususer ))
{
// Send the second 401 response
This. send401 ();
}
Else
{
// Clear the Session of the current user. This will clear all sessions objects including the "authenticationattempts"
Session. Abandon ();
Session. Clear ();
// Redirect back to the main page
Response. Redirect ("~ /Default. aspx ");
}
}
}
/// <Summary>
/// Make sure the browser does not cache this page
/// </Summary>
Public void disablepagecaching ()
{
Response. expires = 0;
Response. cache. setnostore ();
Response. appendheader ("Pragma", "No-Cache ");
}
/// <Summary>
/// Send a 401 response
/// </Summary>
Public void send401 ()
{
// Create a 401 response, the browser will show the log-in dialogbox, asking the user to supply new credentials, // If browser is not set to "automaticaly sign in with current credentials"
Response. Buffer = true;
Response. statuscode = 401;
Response. statusdescription = "unauthorized ";
// A Authentication Header must be supplied. This header can be changed to negotiate when using Keberos Authentication
Response. addheader ("www-authenticate", "NTLM ");
// Send the 401 response
Response. End ();
}
}
}