我的理解一直是方法上的安全属性会覆盖类的安全属性,但下面的简单代码证明了这种情况似乎不再是这样:
class Program
{
[PrincipalPermission(SecurityAction.Demand, Authenticated = true)] //<-- this passes
class DumbClass
{
[PrincipalPermission(SecurityAction.Demand, Role = "ffff")] //<-- this passes (but shouldn't)
public string EchoMethod(string input)
{
return input;
}
}
static void Main(string[] args)
{
Thread.CurrentPrincipal = new ClaimsPrincipal(new ClaimsIdentity("manual"));
//this should throw becuase the principal is not in the role "ffff"
//BUT DOESN'T
Console.WriteLine(new DumbClass().EchoMethod("this"));
}
}
如果我删除了类上的声明,那么我会得到预期的安全性异常。 我错过了一些非常明显的东西。我正在使用.Net 4.5
答案 0 :(得分:0)
答案 1 :(得分:-1)
更改您的代码:
[PrincipalPermission(SecurityAction.Demand)] //<-- REMOVE Authenticated = true
class DumbClass
{
[PrincipalPermission(SecurityAction.Demand, Role = "ffff")] //<-- this passes (but shouldn't)
public string EchoMethod(string input)
{
return input;
}
}
通过设置Authenticated = true,您明确指出用户可能已经过验证或未验证过。