如何为我的网站创建两个管理员用户?

时间:2015-03-30 13:11:28

标签: c# visual-studio-2010 web-applications

我目前在我的网站上有一个名为“SYSTEM”的管理员用户,该用户拥有该网站的完全访问权限,并且能够添加/删除用户凭据。我创建了另一个名为“trainer”的用户,它具有相同的权限级别。但是,当我测试我的webapp时,“培训师”用户受到限制,无法添加/删除用户。我的代码如下,有人可以帮忙吗?

 protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringBuilder SB = new StringBuilder();
        UserModel a = (UserModel)ViewData["UserMaint"];
        List<String> UserList = a.getUserList();
        List<String> CountryList = a.getCountryList();
        SB.Append("<input type=\"hidden\" id=\"UserMaintActionURL\" value=\"");
      //  SB.Append("<input type=\"hidden\" id=\"User\" value=\"");
      //  SB.Append(ApplicationUtility.FormatURL("/Stock/Login"));
        SB.Append("\" />");

        litLoginActionHidden.Text = SB.ToString();

        if (ViewData["ERROR"] != null)
        {
            errormsg.Text = ViewData["ERROR"].ToString();
        }
        else
        {
            errormsg.Text = " ";
        }
        SB = new StringBuilder();
        SB.Append("<input type=\"hidden\" id=\"User\" name=\"User\" value=\"" + a.GetCurrentUser().ToUpper() + "\" />");
        SB.Append("<select name=\"textUser\" id name=\"textUser\" onchange=\"onChangeUser()\">");

        foreach (String element in UserList)
        {
            if (a.GetCurrentUser().ToUpper()==element.ToUpper()||a.GetCurrentUser().ToUpper() == "SYSTEM" || a.GetCurrentUser().ToUpper() == "trainer")
            {
                if (a.GetUser().ToUpper() == element.ToUpper())
                {
                    SB.Append("<option value=\"" + element + "\" selected >" + element + "</option>");
                }
                else
                {
                    SB.Append("<option value=\"" + element + "\">" + element + "</option>");
                }
            }
                    }
        SB.Append("</select>");
        litUserMaint.Text = SB.ToString();
        SB = new StringBuilder();
        SB.Append("<select name=\"textCntry\" id name=\"textCntry\" >");
        if (a.GetCurrentCntry().ToUpper() == "ALL"||a.GetCurrentUser().ToUpper() == "SYSTEM" || a.GetCurrentUser().ToUpper() == "trainer")
        {
            SB.Append("<option value=\"ALL\">ALL</option>");
        }
                    foreach (String element in CountryList)
        {
            if (a.GetCurrentCntry().ToUpper() == element.ToUpper() || a.GetCurrentUser().ToUpper() == "SYSTEM" || a.GetCurrentUser().ToUpper() == "trainer") 
            {
                if (a.GetCountry().ToUpper() == element.ToUpper())
                {
                    SB.Append("<option value=\"" + element + "\" selected >" + element + "</option>");
                }

                else
                {
                    SB.Append("<option value=\"" + element + "\">" + element + "</option>");
                }

            }
        }

        SB.Append("</select>");             
        litCountryList.Text = SB.ToString();
        if (a.GetErrorMessage().Trim() != "")
        {
            StringBuilder ES = new StringBuilder();
            ES.Append("<table border=1><tr><td  class=\"ErrorText\">");
            ES.Append(a.GetErrorMessage().ToString());
            ES.Append("</td></tr></table>");
            errormsg.Text = ES.ToString();
        }
        else
        {
            errormsg.Text = " "; 
        }

1 个答案:

答案 0 :(得分:4)

您的代码中存在逻辑错误。

if (a.GetCurrentUser().ToUpper()==element.ToUpper()||a.GetCurrentUser().ToUpper() == "SYSTEM" || a.GetCurrentUser().ToUpper() == "trainer")

字符串a.GetCurrentUser().ToUpper() == "trainer"永远不会成立。您将左侧的大写字符串评估为小写字符串。

要修复它,您可以写:

a.GetCurrentUser().ToUpper() == "TRAINER"

或者:

a.GetCurrentUser().Equals("trainer", StringComparison.InvariantCultureIgnoreCase))

我还建议只在循环开始时运行GetCurrentUser然后使用结果,如果它是一个昂贵的评估(即使它不是),因为你不需要用同样的方法多次获取它。

关于你的代码主题;还有一些其他问题:

  • 您允许在网站上显示用户名,但不包含HTML编码。如果该用户名曾经显示给其他用户,则有人可能会制作一个恶意用户名,允许他们执行XSS或SQL注入攻击(如果你还错误地没有参数化你的SQL输入,那么某些情况下的SQL注入就是SQL注入)。\

  • 您的代码中有很多内容,尝试手动构建HTML并不好玩。还有其他方法;特别是使用视图。在不了解您的设置的情况下,除了&#34之外,我不能说更多,不要做你正在做的事情&#34;如果你曾经在工作环境中工作过。