如果只有登录用户,如何重定向或访问页面(How to redirect or visit page if only there is a logged in user)

编程入门 行业动态 更新时间:2024-10-26 00:18:21
如果只有登录用户,如何重定向或访问页面(How to redirect or visit page if only there is a logged in user)

我有三个页面( Home, Register, Store )相互链接。 主页针对保存在数据库中的预先存在的用户提供登录选项。 如果只有登录成功,有人可以访问商店页面,否则点击商店页面什么都不做,(只需坚持在同一主页)。

Home.aspx.cs:

protected void Button1_Click(object sender, EventArgs e)//login { SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\lab1.mdf;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM userdata WHERE username='" + TextBox1.Text + "'"; cmd.Connection = conn; //cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { //Session["name"] = dt.Rows[0]["userName"].ToString(); Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); } else { Response.Redirect("Register.aspx"); } }

Home.aspx:

<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>

Store.aspx:

<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>

I have three pages (Home, Register, Store)linked with each other. The home page offers log in option against a pre existing user saved in database. Someone can visit the Store page if only the log in successful, otherwise clicking in the Store page do nothing, (just stick in the same Home page).

Home.aspx.cs:

protected void Button1_Click(object sender, EventArgs e)//login { SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\lab1.mdf;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM userdata WHERE username='" + TextBox1.Text + "'"; cmd.Connection = conn; //cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { //Session["name"] = dt.Rows[0]["userName"].ToString(); Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); } else { Response.Redirect("Register.aspx"); } }

Home.aspx:

<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>

Store.aspx:

<p><a href="Home.aspx">Home</a> <a href="Register.aspx">Register</a> <a href="Store.aspx">Store</a></p>

最满意答案

有两种方法可以创建一个类并定义一个静态字段,另一种方式是session,但我更喜欢session for login

首先,你将创建一个会话,例如

if (dt.Rows.Count > 0) { Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); Session["UserAuthentication"] = true; }

现在检查每个页面或者如果你在.cs文件中有母版页添加代码来检查天气UserAuthentication是否为真,并记得将代码添加到cs文件的Page_Load中

加一张支票

if (Convert.ToBoolean(Session["UserAuthentication"]) == false) { Response.Redirect("Register.aspx"); }

there are two ways one is to create a class and define a static field and other way is session but i would prefer session for login

first you will create a session e.g

if (dt.Rows.Count > 0) { Response.Redirect("Store.aspx?name=" + TextBox1.Text + ""); Session["UserAuthentication"] = true; }

and now check on every page or if you have master page in the .cs file add code to check weather UserAuthentication if true or not and remember to add code into the Page_Load of the cs file

add a check

if (Convert.ToBoolean(Session["UserAuthentication"]) == false) { Response.Redirect("Register.aspx"); }

更多推荐

本文发布于:2023-08-07 23:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   页面   用户   redirect   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!