How to redirect the user to login.aspx when he/she tries to access other pages without first logon to the server?
For eg. User A try to access menu.aspx without logon to the server, then it should redirect the user
to login.aspx.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amr Noureldin10 reputationPosted Aug 14, 2005, 12:27 AM EDT
Do you use sessions? cookies? or what?
Ravi Puri10 reputationPosted Mar 8, 2006, 6:46 AM EST
Ideally you should be taking advantage of ASP.NET inbuilt authentication features.
In doing so,
If you were to configure your web.config as such:
<configuration>
….
…
<location path="MySecuredPages">
<system.web>
<authorization>
<deny users="?"/>
authorization>
system.web>
location>
….
….
configuration>
<authentication mode="Forms">
<forms name="MyApplication" path="/" requireSSL="false" loginUrl="LogIn.aspx" protection="All" timeout="1440" slidingExpiration="false"/>
authentication>
Then upon attempting to request MySecuredPages/Menu.aspx you will automatically be re-directed to Login.aspx.
In Login.aspx take advantage of the System.Web.Security objects such as FormsAuthenticationTicket, FormsAuthentication
Alternatively,
I would recommend you build a custom page that checks for user authentication in the OnInit() event (you must override) and have all your secured pages inherit this page:
public class SecuredPage : Page
{
private bool isAuthorised = false;
public bool IsAuthorised
{
get
{
return isAuthorised;
}
set
{
isAuthorised = value;
}
}
override protected void OnInit(EventArgs e)
{
if (!this.Authorised())
{
try
{
throw new UnauthorizedAccessException();
}
catch (UnauthorizedAccessException uae)
{
Server.Transfer("Login.aspx");
}
}
/
InitializeComponent();
base.OnInit(e);
}
#endregion
private bool Authorised()
{
// check in session OR HttpContext then FormsAuthenticationTicket for userid
}
}
}
public class Menu : SecuredPage
{
…
}