对 AllowPartiallyTrustedCallersAttribute 程序集中的某些类型要求完全信任

更新:2007 年 11 月

在将 AllowPartiallyTrustedCallersAttribute 放置在程序集上时,将禁用要求所有调用方完全受信任的隐式 LinkDemand。在某些情况下,您可能想让程序集中的某些成员或类可被部分受信任的代码调用,但仍保护同一程序集中的其他成员或类不被部分受信任的代码调用。以下步骤使您可以允许部分受信任的代码访问某些成员或类,同时仍旧拒绝部分受信任的代码访问其他成员或类:

  1. 使用强名称为您的代码签名。

  2. AllowPartiallyTrustedCallersAttribute 属性应用于您的程序集以便部分受信任的调用方可以使用它。

  3. 对步骤 2 中的程序集中要保护不让部分受信任的调用方使用的特定成员或类应用完全信任这一要求。

以下要求可放置在您的代码上以限制部分受信任代码的访问:

  • 若要产生需要完全信任的 LinkDemand,请将以下语句应用于类或成员。

    <PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")>
    
    [PermissionSetAttribute(SecurityAction.LinkDemand, Name="FullTrust")]
    
  • 若要产生需要调用方具有调用非托管代码功能的链接要求,请将以下语句应用于类或成员。

    <SecurityPermissionAttribute(SecurityAction.LinkDemand, UnmanagedCode:=True)>
    
    [SecurityPermissionAttribute(SecurityAction.LinkDemand, UnmanagedCode=true)]
    
  • 若要产生需要完全信任的继承要求,请将以下语句应用于类或成员。

    <PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust")>
    
    [PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust")]
    

请参见

概念

通过部分受信任的代码使用库

其他资源

代码访问安全性