首页 > 解决方案 > How to initialize login functionality in Site.Master form to pull Login ID from SQL after redirect?

问题描述

How do I save results of a logged in user after successful redirect from the Login page or Sign up page? I have a button in the Site.Master parent form, which is loaded with every other web form. I want to get the ID of the logged in user from my SQL database and populate a textbox in a form for Reservation.

My project is in ASP.NET about making room reservations. I have a login in button in the Master Form, which consists of a navigation bar with 8 menu buttons each redirecting to a different Web Page. The data in the Login Form is checked with my SQL server, and upon successful login, the user is redirected to the reservation page. My idea was to declare 2 global variables like a bool, checking if user is logged in on every redirect and an empty string, which gets the value of User ID from SQL if bool LoggedIn is true. But I was unsuccessful in initializing the global variables and calling them in the child form (Booking form) and frankly, I'm not sure if I've taken the right approach to achieving this. My code in Login_in.aspx.cs for login redirect is the following:

protected void Button1_Click(object sender, EventArgs e)
        {
            String source = @"Data Source=DESKTOP;Initial Catalog=projectX;Integrated Security=true;";
            SqlConnection con = new SqlConnection(source);

            {
                try
                {
                    string uid = TextBox1.Text;
                    string pass = TextBox2.Text;
                    con.Open();
                    string query = "select * from Users where Username='" + uid + "' and Password='" + pass + "'";
                    SqlCommand cmd = new SqlCommand(query, con);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.Read())
                    {
                        Response.Redirect("~/Booking.aspx");
                    }
                    else
                    {
                        Label10.Text = "Wrong pass or username!";
                    }
                    con.Close();

                    Label10.Visible = true;
                }

                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }

I'm not sure if I have to declare the global variables for bool LoggedIn and string UserID in the Login Form or the Master Form, but I want to change their values upon Response.Redirect("~/Booking.aspx"). If they are declared in the Site.Master.cs file, how can I change their values in the Login form and call them (and their values) in the Booking form. I've tried declaring them as public static bool LoggedIn = false; public static string userID; but wasn't sure how to initialize the get & set properties between forms.

In my Booking form I have a textbox which I want to populate with the username of the loggedIn user by userID (from sql). How do I set the properties in the form where the variables are declared and in the forms, where they are called?

标签: c#asp.netloginwebforms

解决方案


ASP.NET Web 窗体支持两种方法来保护您的 Web 应用程序:

它们都为您提供了对用户进行身份验证和授权以及访问已登录用户的基础设施。他们都使用数据库作为存储。各种技术用于在请求(例如 cookie)之间识别经过身份验证的用户。

强烈建议您在应用程序中使用其中任何一个。有大量文档可供阅读。

另一方面,实施您自己的安全系统将是困难的、容易出错的、耗时的、低效的并且最终是不安全的。


推荐阅读