SecurityCritical on overriden function InitializeL

2019-07-17 13:36发布

问题:

I'm running into some Medium trust issues with a few libraries. I'm able to reproduce the error with a sample and referencing that in my MVC probject. I'm trying to get pass this problem but don't understand what I'm missing.

I keep getting this error:

Inheritance security rules violated while overriding member: 'Temp.Class1.InitializeLifetimeService()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

I think understand the security transparency error above, and I make sure my method is the same as the overriding method. Here is my class:

public class Class1 : MarshalByRefObject
{

    [SecurityCritical]
    public override object InitializeLifetimeService()
    {
        return null;
    }

}

And I am still getting the same error as above.

Have been adding and removing this line with no effect:

[assembly: AllowPartiallyTrustedCallers()]

Reading other articles all I have to do is to add the SecurityCritical attribute to the method, but it does not seem to have any affect.

Any ideas, or something that I'm missing?

回答1:

In a Medium trust web application, only GACed binaries are granted full trust permissions. If your binary is bin-deployed, it is partial-trust and transparent. MSDN has a good write-up of what transparent code can and cannot do. Importantly, declaring a [SecurityCritical] member is a full-trust only operation. If your bin-deployed library contains a member annotated with [SecurityCritical], the CLR will ignore that annotation.

If your library is designed to be bin-deployed and runnable in Medium trust, you cannot override or otherwise access a [SecurityCritical] member. Consider reworking your library so as not to call these methods.