Click here to Skip to main content
15,885,980 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,

Can someone please assist me on how to create session id That force user to login on my website and How to store that id. And how to display login info (Like name,ID etc of user) on every page of website.
Posted
Updated 3-Apr-19 1:09am
v2

Storing and retrieving values from Session
C#
//Storing UserName in Session
Session["UserName"] = txtUser.Text;


C#
//Check weather session variable null or not
if (Session["UserName"] != null)
{
    //Retrieving UserName from Session
    lblWelcome.Text = "Welcome : " + Session["UserName"];
}
else
{
 //Do Something else
}


SessionID:
The SessionID property is used to uniquely identify a browser with session data on the server. The SessionID value is randomly generated by ASP.NET and stored in a non-expiring session cookie in the browser. The SessionID value is then sent in a cookie with each request to the ASP.NET application.
Ref.:HttpSessionState.SessionID Property[^]

Refer:
MSDN:
http://msdn.microsoft.com/en-us/library/6ad7zeeb.aspx[^]
http://msdn.microsoft.com/en-us/library/ms178581.aspx#CodeExamples[^]

How and why session IDs are reused in ASP.NET[^]
ASP Session Object[^]

A good article on CP:
Exploring Session in ASP.NET[^]

More articles on CodeProject[^]
 
Share this answer
 
v2
Comments
Rahul Rajat Singh 11-Jul-12 6:31am    
good answer. +5.
Prasad_Kulkarni 11-Jul-12 6:42am    
Thank you Rahul!
AshishChaudha 11-Jul-12 6:34am    
my 5!
Prasad_Kulkarni 11-Jul-12 6:42am    
Thank you! But where's 5
So my understanding is that you want to use sessions for keeping the user credentials.

Here is a small snippet that does the same
C#
string username = TextBox1.Text;
string password = TextBox2.Text; 

if (user.ValidateUser(username, password)) //assuming this function will take care of checking user in DB
 {
             Session["UserId"] = username;
 }

and to show this on page:
C#
string username = Session["UserId"] as string;
label1.Text = "Welcome" + (username != null)? username : "Guest";

P.S. Here is an entie project that uses this approach to keep track of users: YaBlogEngine - A Tiny Blog Engine written in ASP.NET/C#[^]. You could find more useful code snippets here.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 11-Jul-12 6:43am    
Nice link +5!
Rahul Rajat Singh 11-Jul-12 6:44am    
thanks.
For creating the logged in session ID, you need to take a string variable for that for example ::
C#
string strSessionID;
strSessionID = HttpContext.Current.Session.SessionID;


Your sessionID will store in strSessionID.
and for creatring the session variables for displiying the data like firstname,last name on page/website, go through below format:

C#
session["firstname"] = fistname;
session["lastnane"]  = lastname;


Now you can use your above session variables on any your web pages.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 11-Jul-12 6:28am    
Why a re-post. If you want to add some lines to your answer then use 'Improve solution' widget to modify it. Don't make another solution if you are just updating previous one. Remove one of the entry.
Prasad_Kulkarni 11-Jul-12 7:16am    
+5!
Get Session ID in ASP MVC
C#
var Session = Session.SessionID;


Store Session
C#
HttpContext.Session["CurrentUserSessionID"] = Session.SessionID;
 
Share this answer
 
For creating the session ID ::
C#
string strSessionID;
strSessionIDHt = tpContext.Current.Session.SessionID;

and For displaying login Information you need to store userID,firstname,lastname in session variables.
 
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