PrincipalPermissionAttribute(SecurityAction) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
警告
PrincipalPermissionAttribute is not honored by the runtime and must not be used.
使用指定的 PrincipalPermissionAttribute 初始化 SecurityAction 類別的新執行個體。
public:
PrincipalPermissionAttribute(System::Security::Permissions::SecurityAction action);
[System.Obsolete("PrincipalPermissionAttribute is not honored by the runtime and must not be used.", true, DiagnosticId="SYSLIB0002", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public PrincipalPermissionAttribute (System.Security.Permissions.SecurityAction action);
public PrincipalPermissionAttribute (System.Security.Permissions.SecurityAction action);
[<System.Obsolete("PrincipalPermissionAttribute is not honored by the runtime and must not be used.", true, DiagnosticId="SYSLIB0002", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Security.Permissions.PrincipalPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Security.Permissions.PrincipalPermissionAttribute
new System.Security.Permissions.PrincipalPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Security.Permissions.PrincipalPermissionAttribute
Public Sub New (action As SecurityAction)
參數
- action
- SecurityAction
其中一個 SecurityAction 值。
- 屬性
範例
下列範例示範如何使用 建 PrincipalPermissionAttribute 構函式來要求目前的用戶是系統管理員。
注意
在 Windows Vista 中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行需要您是系統管理員的程式碼,您必須先將權限從標準使用者提高為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Policy;
using namespace System::Security::Principal;
[PrincipalPermission(SecurityAction::Demand, Role = "Administrators")]
void CheckAdministrator()
{
Console::WriteLine("User is an administrator.");
}
int main(array<System::String ^> ^args)
{
try
{
// Must set PrincipalPolicy to WindowsPrincipal
AppDomain::CurrentDomain->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
// Check using declarative security.
CheckAdministrator();
// Check using Imperative security.
System::String^ null;
PrincipalPermission^ principalPerm = gcnew PrincipalPermission(null, "Administrators" );
principalPerm->Demand();
Console::WriteLine("Demand succeeded");
}
catch (Exception ^e)
{
Console::WriteLine(e->Message);
}
return 0;
}
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
class SecurityPrincipalDemo
{
public static void Main()
{
try
{
// PrincipalPolicy must be set to WindowsPrincipal to check roles.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
// Check using the PrincipalPermissionAttribute
CheckAdministrator();
// Check using PrincipalPermission class.
PrincipalPermission principalPerm = new PrincipalPermission(null, "Administrators");
principalPerm.Demand();
Console.WriteLine("Demand succeeded.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
static void CheckAdministrator()
{
Console.WriteLine("User is an administrator");
}
}
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal
Class SecurityPrincipalDemo
Public Shared Sub Main()
Try
' PrincipalPolicy must be set to WindowsPrincipal to check roles.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
' Check using the PrincipalPermissionAttribute
CheckAdministrator()
' Check using PrincipalPermission class.
Dim principalPerm As New PrincipalPermission(Nothing, "Administrators")
principalPerm.Demand()
Console.WriteLine("Demand succeeded.")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
<PrincipalPermission(SecurityAction.Demand, Role:="Administrators")> _
Shared Sub CheckAdministrator()
Console.WriteLine("User is an administrator")
End Sub
End Class
備註
Demand
、 InheritanceDemand
和 LinkDemand
是唯一對這個屬性具有意義的值 SecurityAction 。 其他動作不適用於非程式代碼訪問許可權的許可權。