【问题标题】:How can I create two admin users for my website?如何为我的网站创建两个管理员用户?
【发布时间】:2015-03-30 13:11:28
【问题描述】:

我的网站目前有一个名为“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 = " "; 
        }

【问题讨论】:

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


    【解决方案1】:

    您的代码中有逻辑错误。

    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 输入,则肯定是 XSS,SQL 注入)。 \

    • 您的代码中发生了很多事情,尝试手动构建 HTML 既不好玩。还有其他方法;专门使用视图。在不了解您的设置的情况下,如果您曾经在工作环境中,我只能说“不要做您正在做的事情”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多