【问题标题】:ASP.NET - Can you make a DetailsView where only users with specific roles see some fields as editable?ASP.NET - 你能否制作一个只有具有特定角色的用户才能看到某些字段可编辑的 DetailsView?
【发布时间】:2011-12-16 16:12:16
【问题描述】:

例如,假设我希望只有管理员能够在详细信息视图中查看和编辑 CustomerID,版主可以看到此值但不可编辑,普通用户甚至看不到它。这可能吗?

【问题讨论】:

  • 是的。现在去谷歌,当你有更具体的问题时再回来。
  • 您当然可以做任何事情,您现在如何处理角色?简而言之,在项目数据绑定事件中,您可以根据当前用户的角色集隐藏或显示或绑定控件
  • 前段时间我对此进行了谷歌搜索。当我执行自定义绑定而不是使用传统的 DataSource 时,我必须从所有搜索中拼凑多个结果才能获得答案。

标签: asp.net vb.net security webforms


【解决方案1】:

给你。我尝试了很多谷歌搜索,没有人能清楚地解释它。您必须在 PreRender 事件上执行此操作。请注意,此代码 sn-p 使用 .net 中的 Membership Provider 来检查用户是否处于角色中。如果您有自己的自定义表,则必须编写一个自定义函数来检查用户是否属于您的自定义角色之一。另请注意,此解决方案使用的是 ItemTemplates 而不是 BoundFields。

protected void detailsView_OnPreRender(object sender, EventArgs e)
    {
        if (dvPackage.CurrentMode == DetailsViewMode.Edit)
        {
           //disables/enables a the dropdown for Process Status if the user has the RLIST role
           TextBox txtCustomID = (TextBox)Utilities.FindControlRecursive(dvPackage, "txtCustomID ");
           txtCustomID.Visible = false;
           if (User.IsInRole("Admin"))
           {
              txtCustomID.Visible = true;

         }
     }

这里是查找控制递归函数。免费。

public static Control FindControlRecursive(Control ctlRoot, string sControlId)
    {
        // if this control is the one we are looking for, break from the recursion    
        // and return the control.    
        if (ctlRoot.ID == sControlId)
        {
            return ctlRoot;
        }
        // loop the child controls of this parent control and call recursively.    
        foreach (Control ctl in ctlRoot.Controls)
        {
            Control ctlFound = FindControlRecursive(ctl, sControlId);
            // if we found the control, return it.        
            if (ctlFound != null)
            {
                return ctlFound;
            }
        }// we never found the control so just return null.    
        return null;
    }

【讨论】:

  • 小优化:将txtCustomID的默认设置为可见=false。然后,如果用户是管理员,您只需要进行递归查找。
【解决方案2】:

我建议使用 Web 用户控件或多视图来处理基于用户角色的视图切换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2014-05-16
    • 2017-09-10
    相关资源
    最近更新 更多