ASP.NET中如何根据会话用户类型动态切换头部菜单?
Hey there! Let's break down why your current code isn't working and fix it up properly.
The Core Issues in Your Current Code
- You're querying the entire
Userstable and just grabbing the 5th column of the first row—this has nothing to do with the currently logged-in user! No matter who's on the site, you're always using the first user's type from the database. - You aren't leveraging ASP.NET's
Sessionto target the specific active user, which is how you should identify who's currently logged in. - Your conditional logic uses two separate
ifstatements instead ofelse if, which is unnecessary and could lead to unexpected behavior if edge cases pop up.
Fixed Solution: Target the Current Logged-In User
First, make sure when a user logs in, you store their unique identifier (like UserID) in the session. For example, after validating their credentials:
// After successful login Session["UserID"] = userDataRow["UserID"]; // Replace with your actual user ID field
Then update your CheckUserType method to use this session data to fetch the correct user type:
public void CheckUserType() { // First, check if the user is logged in (session exists) if (Session["UserID"] == null) { // Hide both menus or redirect to login—adjust based on your needs userhome.Visible = false; adminpanel.Visible = false; return; } string connectionString = ConfigurationManager.ConnectionStrings["BoothsConnectionString1"].ConnectionString; using (SqlConnection con = new SqlConnection(connectionString)) { // Query ONLY the current user's type, not the whole table string query = "SELECT UserType FROM Users WHERE UserID = @UserID"; using (SqlCommand cmd = new SqlCommand(query, con)) { // Use parameterized query to prevent SQL injection cmd.Parameters.AddWithValue("@UserID", Session["UserID"]); con.Open(); // Use ExecuteScalar to get the single value we need (more efficient than DataTable) object userTypeResult = cmd.ExecuteScalar(); if (userTypeResult != null) { string userType = userTypeResult.ToString().Trim(); if (userType == "U") { userhome.Visible = true; adminpanel.Visible = false; } else if (userType == "A") { adminpanel.Visible = true; userhome.Visible = false; } else { // Handle unknown user types (hide both or show a default) userhome.Visible = false; adminpanel.Visible = false; } } else { // No user found with that ID—handle accordingly userhome.Visible = false; adminpanel.Visible = false; } } } }
Even Better: Cache User Type in Session
To avoid hitting the database every time you check the user type, store the user type directly in the session when the user logs in:
// After successful login Session["UserID"] = userDataRow["UserID"]; Session["UserType"] = userDataRow["UserType"]; // Store the type here
Then your check method becomes much simpler and faster:
public void CheckUserType() { if (Session["UserType"] == null) { userhome.Visible = false; adminpanel.Visible = false; return; } string userType = Session["UserType"].ToString().Trim(); if (userType == "U") { userhome.Visible = true; adminpanel.Visible = false; } else if (userType == "A") { adminpanel.Visible = true; userhome.Visible = false; } else { userhome.Visible = false; adminpanel.Visible = false; } }
内容的提问来源于stack exchange,提问作者janek jan
相关产品推荐
相关产品推荐

