如何在asp.net和C#中实现登录会话

时间:2022-09-01 23:59:58

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

我是asp.net和C#的新手,我想问一下如何使用asp.net和C#实现会话登录。

Please advise.

Thanks.

3 个解决方案

#1


11  

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

在C#中,您可以像这样定义会话变量:

Session["userame"]= txtusername.Text;

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

其中txtusername是一个文本框。在另一个页面中,您可以将其称为:

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

在特定页面中检查用户是否已登录;你必须检查这个会话是否为空。如果会话为null,则将用户重定向到登录页面,否则他/她可以查看该页面。相同的逻辑适用于要实现会话验证的所有页面。示例(在Page_Load事件上):

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

Hope it helps... :)

希望能帮助到你... :)

#2


4  

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
  • 在sql server或您选择的任何数据库中创建数据库,用户表

  • 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);
  • 如果用户存在且密码匹配则创建会话,如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");

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

#3


3  

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

#1


11  

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

在C#中,您可以像这样定义会话变量:

Session["userame"]= txtusername.Text;

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

其中txtusername是一个文本框。在另一个页面中,您可以将其称为:

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

在特定页面中检查用户是否已登录;你必须检查这个会话是否为空。如果会话为null,则将用户重定向到登录页面,否则他/她可以查看该页面。相同的逻辑适用于要实现会话验证的所有页面。示例(在Page_Load事件上):

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

Hope it helps... :)

希望能帮助到你... :)

#2


4  

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
  • 在sql server或您选择的任何数据库中创建数据库,用户表

  • 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);
  • 如果用户存在且密码匹配则创建会话,如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");

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

#3


3  

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