使用其他类中的字符串在我的Web应用程序中管理权限

时间:2011-12-04 21:38:25

标签: c# .net

我正在使用ASP.NET(C#)开发一个Web应用程序,并且需要为登录某些权限(阅读,编写,编辑)的用户提供帮助。每个权利都分配了一个字母(阅读 - " A",写作 - " B",编辑 - " C")。在SQL表中,这些权限被写为字符串。一个只能阅读的人,有一个字符串" A"而拥有所有权利的人都有字符串" ABC"。

在LogIn表单中(如果登录成功)我从表中读取这些权限并将它们写入字符串(字符串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();            
    }
}

这里我需要使用字符串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;
    }
}

1 个答案:

答案 0 :(得分:2)

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

jeroenh是对的。您应该明确地查看SQL注入,因为您的网站目前很容易受到攻击。 OWASP网站是一个良好的开端。您可以找到有关如何解决问题的一些信息herehere