简体   繁体   中英

C# Populate drop down based on data from cookie

I will see if I can explain this clearly enough. I have 2 web forms. One is a basic Forms Authentication login page and the other form displays tasks from multiple servers. I am creating a cookie that stores the UserID. Here is the code for my cookie:

        FormsAuthenticationTicket tkt = new FormsAuthenticationTicket(1, txtUser.Text, DateTime.Now, DateTime.Now.AddMinutes(120), true, rdr.GetInt32(0).ToString(), FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(tkt);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

On my other form, I have a drop down box that displays all servers by Server IP from the Servers table.

public void Populate()
        {
            SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
            myConnection1.Open();

            SqlCommand cmd1 = new SqlCommand("SELECT ServerIP FROM Servers", myConnection1);
            SqlDataReader dropReader;
            dropReader = cmd1.ExecuteReader();

            drpChoose.DataSource = dropReader;
            drpChoose.DataTextField = "ServerIP";
            drpChoose.DataValueField = "ServerIP";
            drpChoose.DataBind();
        }

I am calling Populate in Page Load. I have another table that stores permissions. It has UserID, ServerID, and Permission (read or execute). Let's say that UserID 1 is associated with only ServerID 1 which has an IP of 192.168.0.10. How can I get this one Server IP to display in the drop down? I am pretty sure if I pass the cookie into the second form that I can take the UserID from that but I do not know where to begin.

I apologize if I have not given enough information. I will provide more if need be.

Looks like you'll need to do a join to your permissions table something like

SELECT ServerIP from Servers s, Permissions p where p.serverid = s.serverid and p.userid = :userIdFromCookie

Then you'll need to pass in the user id from your cookie into the Populate method and use a DbParameter to pass the value into your Sql command.

something like (this is pseudocode by the way as I'm not at my dev machine)

cmd.AddInParameter(":userIdFromCookie",dbType.AnsiString, Request.Cookies["mycookie"]["userid"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM