Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have login page .
now i want like this
when i logged in the page so that user only access his information. not another user. so if any user want to see his information then he has to login the page.

how to put userid in session for different aspx pages
Posted
Comments
_Amy 18-Jul-12 3:16am    
Is this a question? Improve your question so that we can understand correctly.
Karthik Harve 18-Jul-12 3:18am    
whenever user logs in to the application, new session starts. every user will have individual sessions. So, now what is your priblem?
MAU787 18-Jul-12 3:23am    
hi..
you can first get user id or username in session variable after login
like this
Session["UserName"] = UserNameTextBox.Text;
Session["UserId"] = UserId(from database after login);

1 solution

If you put the userId an a session variable, you can access that variable from every page. So let's say you have a "details.aspx" and a querystring telling tha page which user to show, the you can just check on page load that the user is logged in and that it is the correct user.

C#
protected void Page_Load(object sender, EventArgs e)
{
    var userIdToShow = Convert.ToInt32(Request.QueryString["UserId"]);
    var userId = Convert.ToInt32(Session["UserId"]);

    // Check if userIdToShow is same as logged in user
    if(userId != userIdToShow)
    {
        Response.Redirect("Login.aspx");
    }
}
 
Share this answer
 

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