开发者

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

开发者 https://www.devze.com 2023-03-13 22:08 出处:网络
I\'m new to asp.net and C# an开发者_JAVA技巧d I want to ask how to implement a session login using asp.net and C#.

I'm new to asp.net and C# an开发者_JAVA技巧d I want to ask how to implement a session login using asp.net and C#.

Please advise.

Thanks.


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... :)


The question is broad answer, in Simply you can follow like this

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

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


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


Easiest way to implement session is as following:

Session["SessionName"] = Value;

For retrieving value

String variable = Session["SessionName"].ToString();

Note: Session variable can be of any type.

Generally session is used for checking whether the user is logged in or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号