Share via


HOW TO:要求具名使用權限集的使用權限

重要事項重要事項

在 .NET Framework 4 版 中,已經移除對強制 DenyRequestMinimumRequestOptionalRequestRefuse 使用權限要求的執行階段支援。在以 .NET Framework 4 (含) 以後版本為基礎的程式碼上不可以使用這些要求。如需這項變更和其他變更的詳細資訊,請參閱 .NET Framework 4 中的安全性變更

除了要求個別的使用權限 (使用 RequestMinimumRequestOptionalRequestRefuse) 以外,您可以要求下列任何內建的使用權限集合:NothingExecutionFullTrustInternetLocalIntranetSkipVerification。 您不可以要求自訂的具名使用權限集合或 Everything 可修改的內建使用權限集合,因為他們所代表的使用權限隨時都可能改變。 以下的範例將說明要求具名使用權限集合的使用權限所使用的語法。 範例會在 PermissionSetAttribute 上附加一個 Name 值,以代表想要的使用權限集合名稱。

範例

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
'The attribute is placed at the assembly level.
<assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name := "FullTrust")>
Namespace MyNamespace
   Public Class [MyClass]
      Public Sub New()
      End Sub
      
      Public Sub MyMethod()
         'Perform operations that require permissions here.
      End Sub 
   End Class
End Namespace
//The attribute is placed at the assembly level.
using System.Security.Permissions;
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
namespace MyNamespace
{
   using System;
   using System.Runtime.InteropServices;
   
   public class MyClass
   {
      public MyClass()
      {
      }
      public void MyMethod()
      {
      //Perform operations that require permissions here.
      }
   }
}

請參閱

概念

使用屬性擴充中繼資料

要求使用權限

程式碼存取安全性