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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ravi PuriPosted Mar 8, 2006, 11:46 AM
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
{
…
}
Amr NoureldinPosted Aug 14, 2005, 4:27 AM
Do you use sessions? cookies? or what?