SecurityManager.IsGranted() 行为

发布于 2024-07-09 10:47:39 字数 874 浏览 11 评论 0原文

谁能解释一下以下 c# 行为吗? 我编写了一个小型控制台应用程序只是为了了解 CAS,但我似乎无法理解为什么以下代码行会像它们一样工作:

string[] myRoles = new string[] { "role1", "role2", "role3" };
GenericIdentity myIdentity = new GenericIdentity("myUsername", "customAuthType");
GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myRoles);

System.Threading.Thread.CurrentPrincipal = myPrincipal;

Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "role1")));
Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "roleX")));

对于两个 SecurityManager.IsGranted() 调用,输出都是“true”。

如果我然后添加以下行:

 new PrincipalPermission(null, "role1").Demand();
 new PrincipalPermission(null, "roleX").Demand();

第一个需求调用通过,但第二个需求调用(如预期)导致 SecurityException。

为什么 SecurityManager.IsGranted() 调用不为“roleX”权限返回 false?

can anybody please explain the following c# behaviour? I have written a small console application just to learn about CAS, but I can not seem to understand why the following lines of code work like they do:

string[] myRoles = new string[] { "role1", "role2", "role3" };
GenericIdentity myIdentity = new GenericIdentity("myUsername", "customAuthType");
GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myRoles);

System.Threading.Thread.CurrentPrincipal = myPrincipal;

Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "role1")));
Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "roleX")));

The output is "true" for both SecurityManager.IsGranted() calls.

If I then add the following lines:

 new PrincipalPermission(null, "role1").Demand();
 new PrincipalPermission(null, "roleX").Demand();

the first demand call passes, but the second one (as expected) causes a SecurityException.

Why does not the SecurityManager.IsGranted()-call return false for the "roleX" permission?

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

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

发布评论

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

评论(3

阿楠 2024-07-16 10:47:39

在 .NET 4.0 SecurityManager.IsGranted 中已过时。

这就是它的本质,如果你以 .NET 4.0 兼容性进行编译,它会抱怨。

bool isGranted = SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.Infrastructure))

修复方法:

var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
bool isGranted = permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);

参考:
http://www.stringbuilder .net/post/2009/07/31/In-NET-40-SecurityManagerIsGranted-is-obsolete.aspx

In .NET 4.0 SecurityManager.IsGranted has been made obsolete.

This is what it was and if you compile in .NET 4.0 compatibility it will complain.

bool isGranted = SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.Infrastructure))

To fix it:

var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
bool isGranted = permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);

Reference:
http://www.stringbuilder.net/post/2009/07/31/In-NET-40-SecurityManagerIsGranted-is-obsolete.aspx

浅沫记忆 2024-07-16 10:47:39

从类似问题的答案此处出现IsGranted() 仅适用于 CAS 权限,不适用于非 CAS 权限。

文章引用:

SecurityManager.IsGranted() 确定
是否授予许可
检查 CAS 权限
已被授予
行政人员。 自从
WorkTimePermission 是非 CAS
许可,这意味着安全
管理员制定的策略有
对该许可没有影响。
换句话说,没有办法
管理员授予或撤销
[非 CAS 许可]。 所以
SecurityManager.IsGranted() 将
总是返回 false
[非 CAS 许可]。

我花了一段时间才习惯 CAS
与非 CAS 权限相比,以及
意识到像这样的关键短语
仅“安全策略”和“策略”
申请CAS权限。 一旦我得到了
对此感到满意,破译
显然是无辜的帮助条目,例如
SecurityManager.IsGranted 的备注
部分变得更加容易:

“授予权限已确定
根据政策...”

这意味着 - 但没有明确表示
状态 - 该方法仅有效
具有 CAS 权限,因为它是
检查当前的安全策略。
这需要一些时间来适应。

From the answers to a similar question here it appears that IsGranted() only works with CAS permissions, and not non-CAS permissions.

Quotes from article:

SecurityManager.IsGranted() determines
whether a permission is granted by
examining the CAS permissions that
have been granted by the
administrator. Since
WorkingTimePermission is a non-CAS
permission, that means the security
policies set by the administrator have
no impact regarding that permission.
In other words, there is no way for an
administrator to grant or revoke a
[non-CAS permission]. Therefore
SecurityManager.IsGranted() will
always return false for
[non-CAS permission].

and

It took me a while to get used to CAS
vs. non-CAS permissions, and to
realize that key phrases like
"security policies" and "policy" only
apply to CAS permissions. Once I got
comfortable with that, deciphering
apparently innocent help entries like
SecurityManager.IsGranted's Remarks
section became much easier:

"Granting of permissions is determined
by policy..."

This implies - but doesn't explicitly
state - that the method only works
with CAS permissions, because it is
checking the current security policy.
It takes some getting used to.

流殇 2024-07-16 10:47:39

我相信 SecurityManager.IsGranted 主要关注代码需求(程序集等) - 而不是诸如主体权限之类的特定需求。

做你想做的事:

    static bool HasAccess(string role)
    {
        IPrincipal principal = System.Threading.Thread.CurrentPrincipal;
        return principal == null ? false : principal.IsInRole(role);
    }

I believe SecurityManager.IsGranted is mainly looking at code demands (the assembly etc) - not specific demands such as principal permissions.

To do what you want:

    static bool HasAccess(string role)
    {
        IPrincipal principal = System.Threading.Thread.CurrentPrincipal;
        return principal == null ? false : principal.IsInRole(role);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文