【问题标题】:Why does PrincipalPermission with a deny securityaction still allow access?为什么带有拒绝安全操作的 PrincipalPermission 仍然允许访问?
【发布时间】: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


【解决方案1】:

您可以在SecurityAction 的文档中看到Deny 已过时并且:

在 .NET Framework 4 中,已删除运行时支持 强制执行 Deny、RequestMinimum、RequestOptional 和 RequestRefuse 权限请求。这些请求不应在以下代码中使用 基于 .NET Framework 4 或更高版本。

由于您使用的是 .NET 4.5.2 - 您的 Deny 请求被简单地忽略了。

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2013-09-22
    • 2013-06-04
    • 1970-01-01
    • 2020-02-29
    • 2011-06-18
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多