【发布时间】:2017-10-28 12:53:53
【问题描述】:
当我注意到我添加的方法时,我一直在玩身份和主体
[PrincipalPermission(SecurityAction.Deny, Role = "Admin")]
身份为
GenericIdentity identity = new GenericIdentity("JC", "Type1");
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "Admin", "User" });
Thread.CurrentPrincipal = principal;
仍然会被调用而不会抛出 SecurityException,就像它有一个 Demand 安全操作一样。
事实上,即使我这样拼错了角色
[PrincipalPermission(SecurityAction.Deny, Role = "asad")]
它仍然允许我调用该方法而不会抛出太多的问题
问题是,为什么?
我的整个代码:
static void Main(string[] args)
{
GenericIdentity identity = new GenericIdentity("JC", "Type1");
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "Admin", "User" });
Thread.CurrentPrincipal = principal;
UsePrincipal();
}
static void UsePrincipal()
{
Console.WriteLine(Thread.CurrentPrincipal.Identity);
try
{
DevWork();
}
catch
{
Console.WriteLine("You Bad!");
}
Console.ReadKey();
}
[PrincipalPermission(SecurityAction.Deny, Role = "Admin")]
static void DevWork() // Will be executed no matter what the role is
{
Console.WriteLine("You Good!");
Console.ReadKey();
}
【问题讨论】:
-
您使用的是哪个版本的 .NET?
-
.net 框架 4.5.2
标签: c# authentication authorization identity principal