如何使用角色(asp.net)而不对其进行硬编码?

发布于 2024-12-03 03:10:45 字数 571 浏览 0 评论 0原文

创建/删除角色时,我不想修改代码。

if (HttpContext.Current.User.IsInRole("Super Admin") ||
    HttpContext.Current.User.IsInRole("Admin") ||
    HttpContext.Current.User.IsInRole("Support"))
{
    if (HttpContext.Current.User.IsInRole("Admin"))
    {
        ListBox1.DataSource = Roles.GetAllRoles().Except(
            new[] { "Super Admin" });

    }
    if (HttpContext.Current.User.IsInRole("Support"))
    {
        ListBox1.DataSource = Roles.GetAllRoles().Except(
            new[] { "Super Admin", "Admin" });
    }
    fillDropDownCustomers();
}

When Roles are created/deleted I wouldn't want to modify the code.

if (HttpContext.Current.User.IsInRole("Super Admin") ||
    HttpContext.Current.User.IsInRole("Admin") ||
    HttpContext.Current.User.IsInRole("Support"))
{
    if (HttpContext.Current.User.IsInRole("Admin"))
    {
        ListBox1.DataSource = Roles.GetAllRoles().Except(
            new[] { "Super Admin" });

    }
    if (HttpContext.Current.User.IsInRole("Support"))
    {
        ListBox1.DataSource = Roles.GetAllRoles().Except(
            new[] { "Super Admin", "Admin" });
    }
    fillDropDownCustomers();
}

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

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

发布评论

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

评论(4

向地狱狂奔 2024-12-10 03:10:45

将这些值放入静态类中:

public static class MyRoles
{
    public const string Admin = "Admin";
    public const string SuperAdmin = "Super Admin";
    public const string Support = "Support";
}

现在您可以像这样重用它们:

if (HttpContext.Current.User.IsInRole(MyRoles.SuperAdmin) ||
    HttpContext.Current.User.IsInRole(MyRoles.Admin) ||
    HttpContext.Current.User.IsInRole(MyRoles.Support))
{

Put those values in static class:

public static class MyRoles
{
    public const string Admin = "Admin";
    public const string SuperAdmin = "Super Admin";
    public const string Support = "Support";
}

Now you can reuse them like this:

if (HttpContext.Current.User.IsInRole(MyRoles.SuperAdmin) ||
    HttpContext.Current.User.IsInRole(MyRoles.Admin) ||
    HttpContext.Current.User.IsInRole(MyRoles.Support))
{
爱格式化 2024-12-10 03:10:45

角色通过为用户可以执行的操作分配值来发挥作用。角色不会改变,但这些角色的行为会改变。超动态解决方案往往是矫枉过正。

因此,也许您有以下角色

  • 超级管理员
  • 支持
  • 管理员

您可以有不同的操作(这取决于您的系统)

  • 查看
  • 编辑
  • 批准

  • 超级管理员 [查看、编辑、批准]
  • 支持 [查看]
  • 管理员 [查看、编辑]

动态部分出现在动作的分配中。以这种方式做事,你并不关心某人扮演什么角色,而是关心他们有什么行为。行动是这种关系中的动态方面。发出请求时,您将使用用户角色来获取分配给该角色的操作(数据库驱动以使其可修改),

将其合并到您的数据库结构中,如“角色有许多操作”,意味着如果将来情况发生变化,您将需要更新数据库中的关系,而不是代码。

数据库结构可能看起来像这样,取决于您的需求。

  • UserRole [ID、UserName、RoleID](如果为用户分配了多个角色,他们将继承所有操作,这些操作可能会重复,因此选择 DISTINCT 或防止出现这种情况,但我相信前者提供了更大的灵活性,而没有复杂性和限制。注意: UserRole 表可以进一步非规范化以使 UserNames 唯一。)
  • [ID, Name]
  • Action [ID, Name]
  • RoleAction [ID, RoleID, ActionID] (RoleID 和 ActionID 上的唯一键约束)

Role 发出请求后,您识别用户等用户名,然后确定他们所处的角色查询 RoleAction 并从而加载其关联的 Actions

我会使用 enums 作为您的 Action 和 Role 值。这使得使用起来更加容易。为了确保数据库和代码位于接收器中,请确保编写单元测试来协调数据库值与枚举值。

Roles work by assigning a value to something a user can do. The Roles dont change but the behaviour for those roles does. Ultra dynamic solutions tend to be overkill.

So perhaps you have the following roles

  • Super Admin
  • Support
  • Admin

You can have different Actions (This would depend on your system)

  • View
  • Edit
  • Approve

Etc

  • Super Admin [View, Edit, Approve]
  • Support [View]
  • Admin [View, Edit]

The dynamic part comes in the assignment of Actions. Doing things this way you dont care what Role someone is in but what actions they have. The Actions are the dynamic aspect in this relationship. When a request is made you will use the users Role to fetch the assigned Actions to that role (Database Driven to make modifiable)

Incorporating this into your Database structure as "Role has many Actions", means that if things change in the future you will need to update the relationship in the database but not code.

A database structure could look something like this, depends on your needs.

  • UserRole [ID, UserName, RoleID] (If user is assigned more than one role they inherit all actions, which might be duplicated and therefore selected DISTINCT or prevent this scenario, but I believe the former provides greater flexibility without complexity and limitation. NOTE: the UserRole table could be further denormalized to make UserNames unique.)
  • Role [ID, Name]
  • Action [ID, Name]
  • RoleAction [ID, RoleID, ActionID] (Unique Key Constraint on RoleID and ActionID)

When a request is made, you identify the user etc UserName, Then workout which Role(s) they are in by quering the RoleAction and thereby load their associated Actions

I would use enums for your Action and Role values. This makes it easier to work with. To ensure that the Database and Code are in sink, ensure that you write a Unit Test reconcile the database values against the enum values.

不一样的天空 2024-12-10 03:10:45

简单的答案是使用常量。更多上下文答案是使用 IoC 将逻辑推送到另一个类中,您可以在其中管理配置或数据库的关系。

Simple answer is use constants. The more context answer is use IoC to push the logic into another class where you can manage the relationships form config or database.

甜中书 2024-12-10 03:10:45

正如其他答案所建议的那样,作为常量变量。但是,如果您要更改其中一个角色的名称,您仍然需要更改代码并重新发布。

另一种选择是将角色的名称添加到配置文件中。您可以使用应用程序设置或继承自 ConfigurationSection 的自定义配置类。请在此处查看 http://msdn.microsoft.com/en-us /library/2tw134k3.aspx

这样您就可以更改 web.config 文件中的角色名称,并且无需更新任何代码或重新发布项目。

Either as the other answers have suggested, as Constant variables. However you would still need to change the code and republish if you were to change the name of one of the roles.

Another option would be to add the names of the roles into a config file. You would make use of the Application Settings, or a custom config class which inherits from ConfigurationSection. Take a look here for how http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

This way you can change the names of the roles within the web.config file and you would not have to update any code or republish the project.

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