SecurityManager.IsGranted() 行为
谁能解释一下以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 .NET 4.0 SecurityManager.IsGranted 中已过时。
这就是它的本质,如果你以 .NET 4.0 兼容性进行编译,它会抱怨。
修复方法:
参考:
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.
To fix it:
Reference:
http://www.stringbuilder.net/post/2009/07/31/In-NET-40-SecurityManagerIsGranted-is-obsolete.aspx
从类似问题的答案此处出现IsGranted() 仅适用于 CAS 权限,不适用于非 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:
and
我相信 SecurityManager.IsGranted 主要关注代码需求(程序集等) - 而不是诸如主体权限之类的特定需求。
做你想做的事:
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: