基于权限的访问控制

时间:2012-07-26 07:02:10

标签: asp.net security

我正在尝试在ASP.NET中实现基于权限的访问控制。为了实现这一点,我创建了一些数据库表,其中包含有关为哪些角色分配了哪些权限以及哪些角色分配给哪个用户的所有信息。

我正在检查业务访问层中的权限。现在我已经创建了一个检查用户权限的方法。如果用户有权限,那么否则它会重定向到另一个页面。

我想知道以下事情是否可能?

class User
{
    [PremissionCheck(UserID,ObjectName,OperationName)]
    public DataTable GetUser()
    {
        //coding for user
    }
}

我在MVC3中看过它。我可以在ASP.NET中创建它吗?如果是,那我该如何实施呢?

2 个答案:

答案 0 :(得分:2)

任何权限系统都需要两个组件 - 授权和访问控制。授权是证明用户身份的手段。这通常通过某种用户和密码存储来实现,但您可以使用OpenID等系统或​​任意数量的联合身份系统(Active Directory / Kerberos /等)来完成同样的任务。

一旦你知道用户是谁,那么就有了访问控制,这是对该用户的强制执行。

现在,在ASP.NET的情况下,你不能仅仅在某些东西上粘贴属性,因为属性不会运行代码。为了运行验证代码,您需要编写某种插件来为您进行此验证。 Webforms已经支持身份验证和访问控制机制;为什么重新发明轮子?

答案 1 :(得分:1)

  

我想知道以下事情是否可行?

class User {
    [PremissionCheck(UserID,ObjectName,OperationName)]
    public DataTable GetUser()
    {
        //coding for user
    } }

否。在ASP.Net webforms中无法实现

然而,我使用MasterPage,BasePage类和RoleBasedAccessControl数据库模型在一个经典的3层ASP.Net 3.5 Web表单应用程序上实现了基于角色的访问控制。

示例

用户“jtirado”被分配角色“HR-Assistant”,可以访问路线“mywebapp / employee.aspx?id = 1452”来编辑员工(id:1452)数据。

作为“HR-Assistant”,该用户可以更改员工电话号码和电子邮件,可以查看员工工资但不能编辑金额。

电话号码,电子邮件,工资是dabatase字段,由ASPX页面上的“asp.net-control”表示/呈现。所以我想根据用户的角色限制对这些控件的访问。

MasterPage 根据用户指定的角色构建用户有权访问的选项菜单。它被我的所有内部页面使用。

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CargaItemMenu(MnuPrincipal, Convert.ToInt32(Session["IdPais"]), Convert.ToInt32(Session["IdRol"]), Convert.ToInt32(Session["IdUsuario"]));
            Session.Add("MenuDinamico", MnuPrincipal);
            if (MnuPrincipal.Items.Count < 1)
            {
                MenuItem menuItems = new MenuItem();
                menuItems.Text = "Principal";
                menuItems.Value = "1";
                menuItems.NavigateUrl = "";
                menuItems.Selectable = true;
                MnuPrincipal.Items.Add(menuItems);
            }

        }
    }

    private void CargaItemMenu(Menu ctrlmenu, int v_IdPais, int v_IdRol, int v_IdUsuario)
    {
        oBEOpcionRol = new SEGU.Entities.ENOpcionRol();
        oBLOpcionRol = new SEGU.BusinessLogic.BLOpcionRol();
        List<ParametroGenerico> ArrayParam;
        ArrayParam = CargarParamentrosOpcionRol(v_IdPais, v_IdRol, v_IdUsuario);
        List<SEGU.Entities.ENOpcionRol> ListaMenuItems = oBLOpcionRol.ListaxIdPaisxIdRolxIdUsuario(ArrayParam);

        foreach (SEGU.Entities.ENOpcionRol objOpcionRol in ListaMenuItems)
        {
            if (objOpcionRol.IdOpcion.IdOpcion.Equals(objOpcionRol.IdOpcion.IdMenu))
            {
                MenuItem mnuMenuItem = new MenuItem();
                mnuMenuItem.Value = objOpcionRol.IdOpcion.IdOpcion.ToString();
                mnuMenuItem.Text = objOpcionRol.IdOpcion.Nombre.ToString();
                if (objOpcionRol.IdOpcion.RutaFormulario != "")
                {
                    mnuMenuItem.NavigateUrl = objOpcionRol.IdOpcion.RutaFormulario.ToString();// +"?IdOpcion=" + Convert.ToString(objOpcionRol.IdOpcion.IdOpcion);
                }

                if (objOpcionRol.IdOpcion.PageNew == "1")
                {
                    mnuMenuItem.Target = "_blank";
                }

                //mnuMenuItem.Target = "iframePrincipal"
                if (objOpcionRol.IdOpcion.Imagen.Trim() != "")
                {
                    mnuMenuItem.ImageUrl = "Seguridad/ImagenesMenus/" + objOpcionRol.IdOpcion.Imagen.Trim();
                }

                if ((mnuMenuItem.NavigateUrl.Trim().Length > 0))
                {
                    mnuMenuItem.Selectable = true;
                }
                else
                {
                    mnuMenuItem.Selectable = false;
                }
                ctrlmenu.Items.Add(mnuMenuItem);
                AddMenuItem(mnuMenuItem, ListaMenuItems);
            }
        }
    }
    private void AddMenuItem(MenuItem mnuMenuItem, List<SEGU.Entities.ENOpcionRol> listaOpcionRol)
    {
        foreach (SEGU.Entities.ENOpcionRol objOpcionRol in listaOpcionRol)
        {
            if (objOpcionRol.IdOpcion.IdMenu.ToString().Equals(mnuMenuItem.Value) && !objOpcionRol.IdOpcion.IdOpcion.Equals(objOpcionRol.IdOpcion.IdMenu))
            {
                MenuItem mnuNewMenuItem = new MenuItem();
                mnuNewMenuItem.Value = objOpcionRol.IdOpcion.IdOpcion.ToString();
                mnuNewMenuItem.Text = objOpcionRol.IdOpcion.Nombre.ToString();
                if (objOpcionRol.IdOpcion.RutaFormulario != "")
                {
                    mnuNewMenuItem.NavigateUrl = objOpcionRol.IdOpcion.RutaFormulario.ToString();// +"?IdOpcion=" + Convert.ToString(objOpcionRol.IdOpcion.IdOpcion);
                }

                if (objOpcionRol.IdOpcion.PageNew == "1")
                {
                    mnuNewMenuItem.Target = "_blank";
                }

                mnuMenuItem.ChildItems.Add(mnuNewMenuItem);
                //mnuNewMenuItem.Target = "iframePrincipal"
                if (objOpcionRol.IdOpcion.Imagen.Trim() != "")
                {
                    mnuNewMenuItem.ImageUrl = "Seguridad/ImagenesMenus/" + objOpcionRol.IdOpcion.Imagen.Trim();
                }

                if ((mnuNewMenuItem.NavigateUrl.Trim().Length > 0))
                {
                    mnuNewMenuItem.Selectable = true;
                }
                else
                {
                    mnuNewMenuItem.Selectable = false;
                }
                AddMenuItem(mnuNewMenuItem, listaOpcionRol);
            }
        }

    }

BasePage 类检查用户是否有权访问所需页面。所有需要授权的页面都继承自此BasePage类。

public class PaginaBase : System.Web.UI.Page
{
    SEGU.BusinessLogic.BLOpcionRol oBLOpcionRol;

    protected void Page_InitComplete(object sender, System.EventArgs e) {
    string Url = this.Page.AppRelativeVirtualPath;
    oBLOpcionRol = new SEGU.BusinessLogic.BLOpcionRol();
    int b = oBLOpcionRol.AutentificarUrl(Convert.ToInt32(System.Web.HttpContext.Current.Session["IdPais"]), Convert.ToInt32(System.Web.HttpContext.Current.Session["IdUsuario"]), Convert.ToInt32(System.Web.HttpContext.Current.Session["IdRol"]), Url);
    System.Web.HttpContext.Current.Session["IdOpcion"] = b;            
        if( b <= 0 ){
            System.Web.HttpContext.Current.Response.Redirect("~/Seguridad/Acceso.aspx");
        return;
        }
    }
.
.
}

最后,在 Customers.aspx Page_Load 事件中,我调用一个函数( oBLPermisoOpcionRol.ValidarPermisos ),该函数检查哪个接收Page实例作为参数并迭代其控件(例如:DdlClientType,TxtLastName,ChkIsActive)以检查用户可以编辑,启用,禁用或隐藏它们。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetNodosMenu(TrvMenu, "");
            if (this.TrvMenu.Nodes.Count < 1)
            {
                PrimerNodos(this.TrvMenu);
            }
            ListarModuloxAnulado(GvModulo, Convert.ToString(RblAnuladoModuloBusqueda.SelectedValue), Convert.ToInt32(0), Convert.ToInt32(DdlNroPaginaModulo.SelectedValue));

            oBLPermisoOpcionRol = new SEGU.BusinessLogic.BLPermisoOpcionRol();
            oBLPermisoOpcionRol.ValidarPermisos(Page, Convert.ToInt32(Session["IdRol"]), Convert.ToInt32(Session["IdOpcion"]));
        }
    }


public void ValidarPermisos(System.Web.UI.Page v_Page, int v_IdRol, int v_IdOpcion)
{        
    BusinessLogic.BLPermisoOpcionRol oBLPermisoOpcionRol = new BusinessLogic.BLPermisoOpcionRol();
    List<ParametroGenerico> ArrayParam ;
    ArrayParam = CargarParametros(v_IdRol, v_IdOpcion);        
    List<SEGU.Entities.ENPermisoOpcionRol> Lista = oBLPermisoOpcionRol.ListaxIdRolxIdOpcion(ArrayParam);        
    for(int Fila= 0; Fila< Lista.Count; Fila++){
        bool v_Anulado= true;
        if (Lista[Fila].Anulado == "1") {
            v_Anulado = true;
        }else if (Lista[Fila].Anulado == "0") {
            v_Anulado = false;
        }
        bool v_ControlVisibleDisabled = true;
        if (Lista[Fila].VisbleDisabled == "1") // Control Disabled
        {
            v_ControlVisibleDisabled = true;
        }
        else if (Lista[Fila].VisbleDisabled == "0") // Control Visible
        {
            v_ControlVisibleDisabled = false;
        }
        SetControls(v_Page, Lista[Fila].IdPermiso.Control, v_Anulado, v_ControlVisibleDisabled);
    }
}
public void SetControls(System.Web.UI.Control parentControl, string v_Control, bool permitir, bool v_Permitir_ControlVisibleDisabled)
{
    foreach(System.Web.UI.Control c in parentControl.Controls){
        if( (c) is Button ){
            if( ((Button)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((Button)c).Enabled = false;
                    }else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((Button)c).Visible = false;
                    }                        
                }else{                        
                    ((Button)c).Visible = true;
                }
            }
        }else if( (c) is CheckBox ){
            if( ((CheckBox)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((CheckBox)c).Enabled = false;
                    }else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((CheckBox)c).Visible = false;
                    }
                }else{
                    ((CheckBox)c).Visible = true;
                }
            }
        }else if( (c) is Label ){
            if( ((Label)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((Label)c).Enabled = false;
                    }else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((Label)c).Visible = false;
                    }
                }else{
                    ((Label)c).Visible = true;
                }
            }
        }else if( (c) is TextBox ){
            if( ((TextBox)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((TextBox)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((TextBox)c).Visible = false;
                    }
                }else{
                    ((TextBox)c).Visible = true;
                }
            }
        }else if( (c) is GridView ){
            if( ((GridView)c).ID == v_Control ){
                if( permitir == true ){
                     if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((GridView)c).Enabled = false;
                    }else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                         ((GridView)c).Visible = false;
                     }
                }else{
                    ((GridView)c).Visible = true;
                }
            }
        }else if( (c) is ImageButton ){
            if( ((ImageButton)c).ID == v_Control ){
                if (permitir == true)
                {
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((ImageButton)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((ImageButton)c).Visible = false;
                    }
                }
                else
                {
                    ((ImageButton)c).Visible = true;
                }
            }
        }else if( (c) is HyperLink ){
            if( ((HyperLink)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((HyperLink)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((HyperLink)c).Visible = false;
                    }
                }else{                        
                    ((HyperLink)c).Visible = true;
                }
            }
        }else if( (c) is DropDownList ){
            if( ((DropDownList)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((DropDownList)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((DropDownList)c).Visible = false;
                    }
                }else{
                    ((DropDownList)c).Visible = true;
                }
            }
        }else if( (c) is ListBox ){
            if( ((ListBox)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((ListBox)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((ListBox)c).Visible = false;
                    }
                }else{
                    ((ListBox)c).Visible= true;
                }
            }
        }else if( (c) is DataList ){
            if( ((DataList)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((DataList)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((DataList)c).Visible = false;
                    }
                }else{
                    ((DataList)c).Visible = true;
                }
            }
        }else if( (c) is CheckBoxList ){
            if( ((CheckBoxList)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((CheckBoxList)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((CheckBoxList)c).Visible = false;
                    }
                }else{
                    ((CheckBoxList)c).Visible = true;
                }
            }
        }else if( (c) is RadioButton ){
            if( ((RadioButton)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((RadioButton)c).Enabled= false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((RadioButton)c).Visible = false;
                    }
                }else{                        
                    ((RadioButton)c).Visible = true;
                }
            }
        }else if( (c) is RadioButtonList ){
            if( ((RadioButtonList)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((RadioButtonList)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((RadioButtonList)c).Visible = false;
                    }
                }else{                        
                    ((RadioButtonList)c).Visible = true;
                }
            }
        }else if( (c) is Image ){
            if( ((Image)c).ID == v_Control ){
                if( permitir == true ){                        
                    ((Image)c).Visible = false;                        
                }else{                        
                    ((Image)c).Visible = true;
                }
            }
        }else if( (c) is Panel ){
            if( ((Panel)c).ID == v_Control ){
                if (permitir == true)
                {
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((Panel)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((Panel)c).Visible = false;
                    }
                }
                else
                {
                    ((Panel)c).Visible = true;
                }
            }
        }else if( (c) is Table ){
            if( ((Table)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((Table)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((Table)c).Visible = false;
                    }
                }else{
                    ((Table)c).Visible= true;
                }
            }
        }else if( (c) is LinkButton ){
            if( ((LinkButton)c).ID == v_Control ){
                if( permitir == true ){
                    if (v_Permitir_ControlVisibleDisabled == true)
                    {
                        ((LinkButton)c).Enabled = false;
                    }
                    else if (v_Permitir_ControlVisibleDisabled == false)
                    {
                        ((LinkButton)c).Visible = false;
                    }
                }else{                        
                    ((LinkButton)c).Visible = true;
                }
            }

        }else if( (c) is System.Web.UI.HtmlControls.HtmlInputButton ){
            if( ((System.Web.UI.HtmlControls.HtmlInputButton)c).ID == v_Control ){
                if( permitir == true ){
                    ((System.Web.UI.HtmlControls.HtmlInputButton)c).Visible = false;
                    ((System.Web.UI.HtmlControls.HtmlInputButton)c).Attributes.Add("disabled", "disabled");
                }else{
                    ((System.Web.UI.HtmlControls.HtmlInputButton)c).Visible = true;
                    ((System.Web.UI.HtmlControls.HtmlInputButton)c).Attributes.Remove("disabled");
                }
            }

        }else if( (c) is System.Web.UI.HtmlControls.HtmlAnchor ){
            if( ((System.Web.UI.HtmlControls.HtmlAnchor)c).ID == v_Control ){
                if( permitir == true ){
                    ((System.Web.UI.HtmlControls.HtmlAnchor)c).Visible = false;
                    // CType(c, System.Web.UI.HtmlControls.HtmlAnchor).Attributes.Add("disabled", "disabled")
                }else{
                    ((System.Web.UI.HtmlControls.HtmlAnchor)c).Visible = true;
                    //CType(c, System.Web.UI.HtmlControls.HtmlAnchor).Attributes.Remove("disabled") '' etiqueta <a runat="server" ID="id1">
                }
            }

        }else if( (c) is System.Web.UI.HtmlControls.HtmlGenericControl ){
            if( ((System.Web.UI.HtmlControls.HtmlGenericControl)c).TagName.ToUpper() == "DIV".ToUpper() ){
                if( ((System.Web.UI.HtmlControls.HtmlGenericControl)c).ID == v_Control ){
                    if( permitir == true ){
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)c).Visible = false;
                        //CType(c, System.Web.UI.HtmlControls.HtmlGenericControl).Attributes.Add("disabled", "disabled")
                    }else{
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)c).Visible = true;
                        //CType(c, System.Web.UI.HtmlControls.HtmlGenericControl).Attributes.Remove("disabled") '' etiqueta <div runat="server" ID="iddiv">
                    }
                }
            }

        }
        SetControls(c, v_Control, permitir, v_Permitir_ControlVisibleDisabled);
    }
}  

这样,我不必使用if-then语句来检查权限,我可以创建任意数量的角色,给予他们任何权限,而无需更改任何C#代码。

您也可以查看这些帖子:

Is ASP.NET role based security a true role based access control system?

Role-based access control - should I have the permission list in the db as well or just in the code (eg enum)?

How to control access to forms fields on a ASP.Net MVC 3 view?