Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement login session in asp.net and C#

Tags:

asp.net

I'm new to asp.net and C# and I want to ask how to implement a session login using asp.net and C#.

Please advise.

Thanks.

like image 356
domlao Avatar asked Jun 20 '11 09:06

domlao


1 Answers

In C# you can define a session variable like this:

Session["userame"]= txtusername.Text;

where txtusername is a text box. In another page you can call it as:

string usrname = Session["username"].ToString();

To check whether a user is logged in or not, in a particular page; you'll have to check if this session is empty or not. If the session is null then redirect the user to login page else he/she can view the page. Same logic applies to all the pages where you want to implement the session validation. Sample (on Page_Load event):

if (Session["username"] == null)
   Response.Redirect ("Login.aspx");

Hope it helps... :)

like image 105
Jayesh Avatar answered Oct 02 '22 12:10

Jayesh