ASP.NET:基于角色的安全性和页面设计

发布于 2024-08-16 07:17:14 字数 369 浏览 4 评论 0原文

我有一个包含几个网格的页面和一个包含 7 个控件的小表单。网格显示辅助数据。总共有大约 320 行隐藏代码来处理各种事件。我要求一个特定角色只能编辑 7 个字段中的 3 个字段,而有权访问此页面的所有其他角色都可以编辑所有字段。

现在回答我的问题。我们通常采用将基于角色的安全性限制在页面级别的方法,因为通过这样做,.Net 中的安全性变得完全可配置。但在这种情况下,由于这个要求,我打算破例,这是我没有模式的新领域。为了为只能访问 3 个控件的角色创建一个单独的页面,我必须进行大量的代码重复,这使得这不是一个选项 - 即使我将一些内容放入用户控件中,这似乎是不合理的工作量反正。

我的第一个想法是禁用 page_load 事件中当前用户无法访问的所有控件,但这感觉很难看。有更好的方法吗?

I have a page with a couple of grids and a small form with 7 controls. The grids show ancillary data. Overall there are about 320 lines of code-behind that handle various events. I have the requirement that one particular role is only supposed to edit 3 fields out of the 7, whereas all other roles with access to this page can edit all of them.

Now to my question. We generally take the approach to restrict role-based security to the page level, since by doing that security in .Net becomes fully configurable. But in this case, I am about to make an exception because of this requirement, and this is new territory where I have no patterns. The amount of code duplication that I would have to do to create a separate page for the role with access to only 3 controls makes this not an option - even if I put some of the things into user controls, which seems an unjustifiable amount of work anyway.

My first thought was to disable all the controls in the page_load event that are not accessible for the current user, but that feels ugly. Is there a better way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-08-23 07:17:14

您可以对要保护的控件类型进行子类化,并在 OnLoad 事件中确定是否允许用户编辑。

public class ProtectedTextBox : TextBox
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        bool hasAccess = CurrentUserHasAccess(); // TODO
        if (hasAccess)
        {
            Enabled = true;
        }
        else
        {
            Enabled = false;
        }
    }
}

然后为 web.config 中的控件分配一个前缀:

<pages>
    <controls>
        <add tagPrefix="me" Namespace="YourNamespace" assembly="YourAssembly" />
    </controls>
</pages>

最后,只需在页面上使用它来代替您想要防止未经授权的用户修改的任何字段的常规文本框。

<me:ProtectedTextBox runat="server" <!-- etc. -->>

对按钮以及您需要的任何其他控件类型执行相同的操作。

You could subclass the control types you want to protect, and determine in the OnLoad event whether to allow the user to edit.

public class ProtectedTextBox : TextBox
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        bool hasAccess = CurrentUserHasAccess(); // TODO
        if (hasAccess)
        {
            Enabled = true;
        }
        else
        {
            Enabled = false;
        }
    }
}

Then assign a prefix to the control in your web.config:

<pages>
    <controls>
        <add tagPrefix="me" Namespace="YourNamespace" assembly="YourAssembly" />
    </controls>
</pages>

Finally, just use it on your page in place of a regular TextBox for any field you want to prevent unauthorized users from modifying.

<me:ProtectedTextBox runat="server" <!-- etc. -->>

Do the same for Buttons as well as whatever other control types you need.

匿名的好友 2024-08-23 07:17:14

如果您创建另一个也继承自同一代码隐藏文件的表单会怎么样?这不会给您带来不同的观点并防止您形成重复的代码吗?

What if you made another form that also inherits from the same code behind file? Wouldn't that give you a different view and prevent you form duplicating code?

烟柳画桥 2024-08-23 07:17:14

您能否安排控件,使受安全性影响的控件位于单独的面板中,然后根据安全性隐藏该面板?

Can you arrange the controls so the ones that are effected by security are in a separate panel, then hide the panel based on security?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文