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

I am new to asp.net and C #, and I want to ask how to implement session login using asp.net and C #.

Please inform.

Thanks.

+6
source share
3 answers

In C #, you can define a session variable as follows:

Session["userame"]= txtusername.Text; 

where txtusername is the text box. On another page you can name it:

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

To check whether a user is registered or not, on a specific page; you will need to check if this session is empty or not. If the session is zero, redirect the user to the login page, otherwise he / she will be able to view the page. The same logic applies to all pages on which you want to implement session verification. Example (in the Page_Load event):

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

Hope this helps ... :)

+7
source
 Session["login_user"] = "[username]"; string username = Session["login_user"].ToString().Trim(); 
+3
source

The question is a broad answer, just you can follow as follows

  • Create a database, a user table on the sql server, or any other database of your choice.
  • Create a login form with user ID and password
  • Check them with a database for user availability.
  • If the user exists and matches the password, create a session, for example Session.Add ("Userid", txtUserid.Text);
  • On other pages (restricted pages where only registered users are allowed), write this code in each page load event

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

+2
source

Source: https://habr.com/ru/post/890909/


All Articles