.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how implement this concept??

Posted By: Jayakumar     Posted Date: August 11, 2009    Points:2   Category :ASP.Net
hai

for example i have ten pages in .aspx application.
if an unknown user try to access the nth(for eg 4th page) directly,how to prevent this.
if v use session validation, its possible to use session validation for every ten pages.
it will take more code know?. how to simplify this.

regards
jai

Responses
Author: Venkat             
Posted Date: August 11, 2009     Points: 5   

Hi,
Yes the best way to use using session But you can simplify the code like this to create your own base class page that check that user is loggied in or not using session check.

Create one base page and for that the code is

In the below example we have used Session["UserId"] to store the userid in the session variable. you can fill this variable in the login page itself


using System;
using System.Web.UI;
public class MyBasePage : System.Web.UI.Page
{

override protected void OnInit(EventArgs e)
{
//initialize our base class (System.Web,UI.Page)
base.OnInit(e);
//check to see if the Session is null (doesn't exist)
if (Context.Session["UserId"] == null)
{
//New User and not logged in so redirect into login page
Response.Redirect("login.aspx");
}
}
}

Now call this class in all your pages in your application using inheritance wherever you want to check that user is logged in or not
eg
Suppose you want to check in default4.aspx than go to cs file of default4.aspx and in the code beside class declaration like this


public partial class default4 : MyBasePage
{
// your rest class logic goes here
}


Mark As Answer if this solve your problem
Warm Regards,
Venkat
Author: Hefin Dsouza             
Posted Date: August 11, 2009     Points: 5   

You can also Write the Code on In The
Application_BeginRequest event of the Global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
{
if(Session["usr"]==null)
{
Response.Redirect("Login.aspx");
}
}


Author: Nabin             
Posted Date: September 03, 2009     Points: 5   

Hi, Jayakumar
i think the better way is that write the above code insideApplication_BeginRequest event of the Global.asax file, as hefin says.


thanks
Nabin



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend