使用来自另一个类的字符串管理我的web应用程序中的权限



我正在开发一个web应用程序在ASP。并且需要给登录的用户一些权限(读、写、编辑)。每一种权利都有一个字母(读"a",写"B",编辑"C")。在SQL表中,这些权限被写成字符串。只有阅读能力的人拥有字符串"A",而拥有所有权限的人拥有字符串"ABC"。

在登录表单(如果登录成功)我从表中读取这些权限并将它们写入字符串(string myRights)。现在在我的主页上,我需要从这个字符串读取,使一些面板可见和其他不可见,根据用户的权利。我如何从另一个类调用字符串?

下面是我使用的一些代码:

public partial class LogIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) 
    {
    }
    protected void btnLog_Click(object sender, EventArgs e)
    {
        string myRights = null;
        SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
        myConnection.Open();
        try
        {
            //Password is encripted, so here is some more code that is not relevant for my problem
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("SELECT * FROM myTable WHERE username='" + textboxUsr.Text + "' AND password='" + textboxPwd.text + "'", myConnection);
            myReader = myCommand.ExecuteReader();
            if (myReader.Read())
            {
                //There is some more code here that is not relevant to this problem
                myRights = myReader["rights"].ToString();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        myConnection.Close();            
    }
}

这里我需要使用string myRights:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
    //Here is some more code, that redirects users back to Login screen if they are not loged in...
    PanelMenuRead.Visible = false;
    PanelMenuWrite.Visible = false;
    PanelMenuEdit.Visible = false;
    //HERE IS THE PART I DO NOT NOW HOW. I need to call string myRights from LogIn class
    if (myRights.IndexOf("A") != -1)
    {
        PanelMenuRead.Visible = true;
    }
    if (pravice.IndexOf("B") != -1)
    {
        PanelMenuWrite.Visible = true;
    }
    if (pravice.IndexOf("C") != -1)
    {
        PanelMenuEdit.Visible = true;
    }
}

可以使用会话在页面之间传递数据。这里是一个快速教程。

jeroenh是对的。您应该仔细检查一下SQL注入,因为您的网站目前很容易受到攻击。OWASP网站是一个良好的开端。你可以在这里和这里找到一些如何解决这个问题的信息。

最新更新