Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i have 25-30 pages
n after login the page can be access.
but i want when user type the url of any inside page, he should be redirect to login page if he is not logged in
is there anyway i can achive this
or i have to check the condition in each n every page_load event of all page

plz reply asap.
also if session is expired or its null then redirect to login page.

all this i want to do in single place instead of each form.....
Posted

For Session expire see this..
Session Timeout Warning and Redirect[^]

make login page as default home page by right clicking the login.aspx
 
Share this answer
 
I think you can do this by using membership and roles of ASP.NET. Putting a check on each page load is rather not recommended instead have a web.config to do the same. read this to get more:

Understanding ASP.NET Roles and Membership - A Beginner's Tutorial[^]
 
Share this answer
 
There are two other methods

1. Use a master page, and place the code within the page load there.
2. You can add a Global.asax class and handle the Session Start/End there.

You should be able to redirect the user to the login page on session start and end.


EDIT:
Here is the code for the Global.asax to check as requested

VB:
VB
Private Sub Global_asax_AcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.AcquireRequestState
       If HttpContext.Current.Session Is Nothing Then
           Return
       End If

       Dim requestedUri As System.Uri = HttpContext.Current.Request.Url
       Dim baseUri As New System.Uri(requestedUri.AbsoluteUri.Substring(0, requestedUri.AbsoluteUri.Length() - requestedUri.PathAndQuery.Length))

       '' They are either logged in, or are requesting the login page
       If requestedUri.AbsoluteUri = baseUri.AbsoluteUri & "login.aspx" OrElse HttpContext.Current.Session("UserLoggedInAttribute") = True Then
           Return
       End If


       HttpContext.Current.Response.Redirect("login.aspx", True)

   End Sub



C# - you need to add the handler
C#
private void Global_asax_AcquireRequestState(object sender, System.EventArgs e)
{
	if (HttpContext.Current.Session == null) {
		return;
	}

	System.Uri requestedUri = HttpContext.Current.Request.Url;
	System.Uri baseUri = new System.Uri(requestedUri.AbsoluteUri.Substring(0, requestedUri.AbsoluteUri.Length() - requestedUri.PathAndQuery.Length));

	//' They are either logged in, or are requesting the login page
	if (requestedUri.AbsoluteUri == baseUri.AbsoluteUri + "login.aspx" || HttpContext.Current.Session["UserLoggedInAttribute"] == true) {
		return;
	}
	HttpContext.Current.Response.Redirect("login.aspx", true);

}
 
Share this answer
 
v2
Comments
sanchu90 29-May-12 7:49am    
thnx for suggestion...
i it will work (for master)

I already tried session in global.asax but it gives me error
so it will be gr8 if you can provide code sample..... :)
MugiwaraUK 30-May-12 12:14pm    
added the sample

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900