what are A, B, C??In the second case, the Common Language Runtime (CLR) will demand that the principal associated with the calling THREAD is in the "Claims Approver" role and will generate a SecurityException if the demand is not satisfied. Note that PrincipalPermissionAttribute is NOT a context attribute, so your object does not need to be context-bound to have this security check performed on its behalf. This functionality is in the runtime itself

解决方案 »

  1.   

    to 思归:
    A,B,C分别是三个函数,A中的代码调用函数B,B然后又调用函数C,但C中一段代码要求权限检查,如果才用第二种方法授权失败CLR可以捕获异常抛出“请求主体权限失败”,现在的问题是,二种方法授权失败后,调用堆栈是如何工作的,有什么区别?
      

  2.   

    >>>二种方法授权失败后,调用堆栈是如何工作的,有什么区别?
    1.手动授权
    it is up to you to decide what to do, since it does nothing if User not in the role of "Claims Approver"2.声明式授权
    CLR throws a SecurityException, it wil rewind to wherever you have a handler or crash your system. You'd better use a try/catch in B
      

  3.   

    采用2方式授权,在C函数处授权失败后,B后续代码以及A后续代码都将中止执行,这样看来岂不是1方式方便(因为它不会抛出异常,不会终止程序)???
      

  4.   

    sure, but you are looking at them at the code level, as a matter of fact, it is more about program designthe first method is called "imperative security check", because you wrote code to perform the check explicitly, it is powerful and flexible in complex situations where you want to take the control, but you have to weave the security code into your applicationthe second method is called "declarative security check", it separates the security code from the application, it is cleaner
      

  5.   

    我看了一下,命令式检查如下:
    PrincipalPermission CEOPermission = new PrincipalPermission("Bob","CEO");
    PrincipalPermission MgtPermission = new PrincipalPermission("mike","Senior Manager");
    (CEOPermission.Union(MgtPermission)).Demand();
    手动检查如下:
    If(HttpContext.Current.User.IsInRole( “Claims Approver” ))
    {
       // Permit access to some code
    }
    声明式检查如下:
    [PrincipalPermissionAttribute (SecurityAction.Demand, Role="Claims Approver")]
    public void ApproveClaims()
    {
    //只有"Claims Approver"的成员能执行此函数
    }
    这样看来如果采用手动检查,可能会使功能函数被其他非信任代码调用,这可能是这种方法的缺陷.是么?
      

  6.   

    I could be wrong, but in my point of view, use code to check security or embed the security information in metadata and have CLR to check? it is more about program design