.NET Framework 开发人员指南
对 AllowPartiallyTrustedCallersAttribute 程序集中的某些类型要求完全信任

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

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

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

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

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

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

    Visual Basic
    <PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")>

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

    Visual Basic
    <SecurityPermissionAttribute(SecurityAction.LinkDemand, UnmanagedCode:=True)>

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

    Visual Basic
    <PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust")>

    C#
    [PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust")]

请参见

标记 :


Page view tracker