Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use session for user log in asp.net?
Posted

Please refer following url,

After successful login add following code to add user data in session.
C#
HttpContext.Current.Session["UserID"] = data.UserId;
        HttpContext.Current.Session["Email"] = data.Email;


Add following code in master page.
C#
public bool checkAuthentication
    {
        get;
        set;
    } 

protected void Page_Load(object sender, EventArgs e)
    {        
        if (checkAuthentication)
        {
            if ((Session["UserID"] + "") == "")
            {
                Response.Write("<script type=\"text/javascript\">" +
                        "window.parent.location = '" + ConfigurationManager.AppSettings["SiteUrl"] + "login?loginUrl=' + window.parent.location + '&mode=session';" +
                                "</script>");
                Response.End();
            }
        }
}


Add following code in each child page where you want to authenticate user session.

C#
protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Master.checkAuthentication = true;
    }


Hope this may help you.
 
Share this answer
 
like this

C#
     //set session like this
                    Session["Username"] = LoginUser.UserName;
                    Session["Password"] = LoginUser.Password;
//validate like this
            if (Session["Username"] != null && Session["Password"] != null)
            {
               string user = Session["Username"].ToString();
               string pass = Session["Password"].ToString();
            }
             else
             {
              Response.Redirect("Login.aspx");
             }
 
Share this answer
 
v3
Comments
Bnod19 22-Mar-12 5:42am    
im sorry..i want i wanted to know from zero state. I mean How to Redirect to other page,on which event should i type the code..plz help me..
Bojjaiah 22-Mar-12 9:29am    
see updated code
Read this[^]

and
Exploring Session in ASP.NET[^]

hope it helps :)
 
Share this answer
 
v2

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