gpt4 book ai didi

asp.net-mvc - 基于Active Directory角色的权限

转载 作者:行者123 更新时间:2023-12-04 08:56:01 25 4
gpt4 key购买 nike

我有一个MVC应用程序,该应用程序使用窗体身份验证对Active Directory进行身份验证。随着我的MVC应用程序规模越来越大,很明显,我正在做的角色检查越来越分散。我想替换以下内容:

[Authorize(Roles = "Staff")]Thread.CurrentPrincipal.IsInRole("Staff")

与以下类似:

[AuthorizePermission(Permission.CanDoSomething)]Thead.CurrentPrincipal.HasPermission(Permission.CanDoSomething)

其中Permission是一个枚举。现在,我想到可以定义每个AD角色在web.config中具有的权限,如下所示:

  <role name="Staff">
<permissions>
<add name="CreateEditDeleteSomething" />
<add name="PublishSomething" />
<add name="QueryUsers" />
</permissions>
</role>


然后,我可以实现IPrincipal扩展方法- HasPermission(Permission permission)。这将查看用户是否将任何人都网志到具有web.config中定义的传入权限的任何AD组。这将使我能够更改特定AD组具有的权限,而不必更改代码或更新现有测试。然后,自定义的Authorize Attribute可以调用HasPermission方法。

这种方法是正确的还是有更好的方法来简化我在应用程序中的角色?我在这里和网上已经看到了许多示例,但是它们似乎过于复杂。我可以仅通过对照HasPermission中的web.config角色设置检查传入的Permission来实现这一点吗? IPrincipal将具有其AD角色,因此确定要确定允许的权限很简单吗?

任何帮助表示赞赏!

最佳答案

权限比角色复杂得多。

通常可以有权限组,例如CreateEditDelete ...,但它们也可以是细粒度的子集,例如“创建”,“编辑”,“删除”

解决该问题的方法是创建一个PermissionsManger类,该类可以确定用户应为业务规则上下文及其AD角色赋予哪些权限。

我使用按位标志来帮助简化精细权限的复杂性。

如何将角色映射到权限完全取决于您。

using System;
using System.Linq;
using System.Security.Principal;

// Install-Package FluentAssertions -Pre
using FluentAssertions;

public static class ExtensionsForIPrincipal
{
public static bool HasPermission(this IPrincipal principal, Permissions permission)
{
return PermissionsManager.GetUserPermissions(principal).HasFlag(permission);
}

public static bool IsInRole(this IPrincipal principal, params string[] roleNames)
{
return roleNames.Any(principal.IsInRole);
}
}

public static class PermissionsManager
{
public static Permissions GetUserPermissions(IPrincipal user)
{
if ( user.IsInRole("admin") )
{
return Permissions.All;
}

var userPermissions = Permissions.None;

if ( user.IsInRole("staff", "user") )
{
userPermissions |= Permissions.QueryUsers;
}

if ( user.IsInRole("staff") )
{
userPermissions |= Permissions.PermissionsCreateEditDeleteSomething | Permissions.QueryUsers;
}

if ( user.IsInRole("editor") )
{
userPermissions |= Permissions.PublishSomething;
}

return userPermissions;
}
}

[Flags]
public enum Permissions
{
None = 0,
CreateSomething = 1,
EditSomething = 2,
DeleteSomething = 4,
PublishSomething = 8,
QueryUsers = 16,
PermissionsCreateEditDeleteSomething = CreateSomething | EditSomething | DeleteSomething,
All = PermissionsCreateEditDeleteSomething | PublishSomething | QueryUsers
}

internal class Program
{
private static void Main(string[] args)
{
IPrincipal admin = Create("james", "admin");

PermissionsManager.GetUserPermissions(admin).ShouldBeEquivalentTo(Permissions.All);

admin.HasPermission(Permissions.None).Should().BeTrue();
admin.HasPermission(Permissions.EditSomething).Should().BeTrue();
admin.HasPermission(Permissions.PermissionsCreateEditDeleteSomething).Should().BeTrue();
admin.HasPermission(Permissions.PublishSomething).Should().BeTrue();
admin.HasPermission(Permissions.QueryUsers).Should().BeTrue();
admin.HasPermission(Permissions.All).Should().BeTrue();

IPrincipal editor = Create("susan", "editor", "staff");

editor.HasPermission(Permissions.None).Should().BeTrue();
editor.HasPermission(Permissions.EditSomething).Should().BeTrue();
editor.HasPermission(Permissions.PermissionsCreateEditDeleteSomething).Should().BeTrue();
editor.HasPermission(Permissions.QueryUsers).Should().BeTrue();
editor.HasPermission(Permissions.PublishSomething).Should().BeTrue();
editor.HasPermission(Permissions.All).Should().BeTrue();

IPrincipal staff = Create("michael", "staff");

staff.HasPermission(Permissions.None).Should().BeTrue();
staff.HasPermission(Permissions.EditSomething | Permissions.DeleteSomething).Should().BeTrue();
staff.HasPermission(Permissions.PermissionsCreateEditDeleteSomething).Should().BeTrue();
staff.HasPermission(Permissions.QueryUsers).Should().BeTrue();
staff.HasPermission(Permissions.PublishSomething).Should().BeFalse();
staff.HasPermission(Permissions.All).Should().BeFalse();

IPrincipal user = Create("bob", "user");

user.HasPermission(Permissions.None).Should().BeTrue();
user.HasPermission(Permissions.EditSomething).Should().BeFalse();
user.HasPermission(Permissions.PermissionsCreateEditDeleteSomething).Should().BeFalse();
user.HasPermission(Permissions.QueryUsers).Should().BeTrue();
user.HasPermission(Permissions.PublishSomething).Should().BeFalse();
user.HasPermission(Permissions.All).Should().BeFalse();

IPrincipal anon = Create("anonymous");

anon.HasPermission(Permissions.None).Should().BeTrue();
anon.HasPermission(Permissions.EditSomething).Should().BeFalse();
anon.HasPermission(Permissions.PermissionsCreateEditDeleteSomething).Should().BeFalse();
anon.HasPermission(Permissions.QueryUsers).Should().BeFalse();
anon.HasPermission(Permissions.PublishSomething).Should().BeFalse();
anon.HasPermission(Permissions.All).Should().BeFalse();

Console.WriteLine("All tests passed");
Console.ReadLine();
}

private static IPrincipal Create(string name, params string[] roles)
{
return new GenericPrincipal(new GenericIdentity(name), roles);
}
}

关于asp.net-mvc - 基于Active Directory角色的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com