語言

WindowsPrincipal 類別

定義

啟用程式碼檢查 Windows 使用者的 Windows 群組成員身份。

public ref class WindowsPrincipal : System::Security::Claims::ClaimsPrincipal
public ref class WindowsPrincipal : System::Security::Principal::IPrincipal
public class WindowsPrincipal : System.Security.Claims.ClaimsPrincipal
[System.Serializable]
public class WindowsPrincipal : System.Security.Principal.IPrincipal
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class WindowsPrincipal : System.Security.Principal.IPrincipal
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class WindowsPrincipal : System.Security.Claims.ClaimsPrincipal
type WindowsPrincipal = class
    inherit ClaimsPrincipal
[<System.Serializable>]
type WindowsPrincipal = class
    interface IPrincipal
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type WindowsPrincipal = class
    interface IPrincipal
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type WindowsPrincipal = class
    inherit ClaimsPrincipal
Public Class WindowsPrincipal
Inherits ClaimsPrincipal
Public Class WindowsPrincipal
Implements IPrincipal
繼承
WindowsPrincipal
繼承
WindowsPrincipal
屬性
實作

範例

以下範例示範如何使用 IsInRole 方法過載。 列 WindowsBuiltInRole 舉作為相對識別碼(RID)的來源,用以識別內建角色。 RID用來決定現任校長的角色。

public:
   static void DemonstrateWindowsBuiltInRoleEnum()
   {
      AppDomain^ myDomain = Thread::GetDomain();

      myDomain->SetPrincipalPolicy( PrincipalPolicy::WindowsPrincipal );
      WindowsPrincipal^ myPrincipal = dynamic_cast<WindowsPrincipal^>(Thread::CurrentPrincipal);

      Console::WriteLine( "{0} belongs to: ", myPrincipal->Identity->Name );

      Array^ wbirFields = Enum::GetValues( WindowsBuiltInRole::typeid );

      for each ( Object^ roleName in wbirFields )
      {
         try
         {
            Console::WriteLine( "{0}? {1}.", roleName,
               myPrincipal->IsInRole(  *dynamic_cast<WindowsBuiltInRole^>(roleName) ) );
         }
         catch ( Exception^ ) 
         {
            Console::WriteLine( "{0}: Could not obtain role for this RID.",
               roleName );
         }
      }
   }
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;

class SecurityPrincipalDemo
{
    public static void DemonstrateWindowsBuiltInRoleEnum()
    {
        AppDomain myDomain = Thread.GetDomain();

        myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
        Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString());
        Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole));
        foreach (object roleName in wbirFields)
        {
            try
            {
                // Cast the role name to a RID represented by the WindowsBuildInRole value.
                Console.WriteLine("{0}? {1}.", roleName,
                    myPrincipal.IsInRole((WindowsBuiltInRole)roleName));
                Console.WriteLine("The RID for this role is: " + ((int)roleName).ToString());
            }
            catch (Exception)
            {
                Console.WriteLine("{0}: Could not obtain role for this RID.",
                    roleName);
            }
        }
        // Get the role using the string value of the role.
        Console.WriteLine("{0}? {1}.", "Administrators",
            myPrincipal.IsInRole("BUILTIN\\" + "Administrators"));
        Console.WriteLine("{0}? {1}.", "Users",
            myPrincipal.IsInRole("BUILTIN\\" + "Users"));
        // Get the role using the WindowsBuiltInRole enumeration value.
        Console.WriteLine("{0}? {1}.", WindowsBuiltInRole.Administrator,
           myPrincipal.IsInRole(WindowsBuiltInRole.Administrator));
        // Get the role using the WellKnownSidType.
        SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
        Console.WriteLine("WellKnownSidType BuiltinAdministratorsSid  {0}? {1}.", sid.Value, myPrincipal.IsInRole(sid));
    }

    public static void Main()
    {
        DemonstrateWindowsBuiltInRoleEnum();
    }
}
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal

Class SecurityPrincipalDemo

    Public Shared Sub DemonstrateWindowsBuiltInRoleEnum()
        Dim myDomain As AppDomain = Thread.GetDomain()

        myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim myPrincipal As WindowsPrincipal = CType(Thread.CurrentPrincipal, WindowsPrincipal)
        Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString())
        Dim wbirFields As Array = [Enum].GetValues(GetType(WindowsBuiltInRole))
        Dim roleName As Object
        For Each roleName In wbirFields
            Try
                ' Cast the role name to a RID represented by the WindowsBuildInRole value.
                Console.WriteLine("{0}? {1}.", roleName, myPrincipal.IsInRole(CType(roleName, WindowsBuiltInRole)))
                Console.WriteLine("The RID for this role is: " + Fix(roleName).ToString())

            Catch
                Console.WriteLine("{0}: Could not obtain role for this RID.", roleName)
            End Try
        Next roleName
        ' Get the role using the string value of the role.
        Console.WriteLine("{0}? {1}.", "Administrators", myPrincipal.IsInRole("BUILTIN\" + "Administrators"))
        Console.WriteLine("{0}? {1}.", "Users", myPrincipal.IsInRole("BUILTIN\" + "Users"))
        ' Get the role using the WindowsBuiltInRole enumeration value.
        Console.WriteLine("{0}? {1}.", WindowsBuiltInRole.Administrator, myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
        ' Get the role using the WellKnownSidType.
        Dim sid As New SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, Nothing)
        Console.WriteLine("WellKnownSidType BuiltinAdministratorsSid  {0}? {1}.", sid.Value, myPrincipal.IsInRole(sid))

    End Sub

    Public Shared Sub Main()
        DemonstrateWindowsBuiltInRoleEnum()

    End Sub
End Class

備註

WindowsPrincipal 類別主要用於檢查Windows使用者的角色。 WindowsPrincipal.IsInRole方法重載讓你可以透過不同的角色上下文來檢查使用者角色。

建構函式

名稱 Description
WindowsPrincipal(WindowsIdentity)

使用指定的 WindowsPrincipal 物件,初始化 WindowsIdentity 類別的新實例。

屬性

名稱 Description
Claims

會取得一個包含所有與該理賠主體相關的所有理賠身份的集合。

(繼承來源 ClaimsPrincipal)
CustomSerializationData

包含由派生型別提供的額外資料。 通常在呼叫 WriteTo(BinaryWriter, Byte[])時設定。

(繼承來源 ClaimsPrincipal)
DeviceClaims

所有 Windows 裝置的聲明都來自此原則。

Identities

會取得一個包含所有與該主張主體相關的權利要求身份的集合。

(繼承來源 ClaimsPrincipal)
Identity

取得現任負責人的身份。

UserClaims

從這個原則中取得所有 Windows 使用者的權利要求。

方法

名稱 Description
AddIdentities(IEnumerable<ClaimsIdentity>)

將指定的權利要求身份加入該權利要求主體。

(繼承來源 ClaimsPrincipal)
AddIdentity(ClaimsIdentity)

將指定的權利要求身份加入該權利要求的主體。

(繼承來源 ClaimsPrincipal)
Clone()

回傳此實例副本。

(繼承來源 ClaimsPrincipal)
CreateClaimsIdentity(BinaryReader)

建立一個新的理賠身份。

(繼承來源 ClaimsPrincipal)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FindAll(Predicate<Claim>)

檢索所有與指定謂詞匹配的權利要求。

(繼承來源 ClaimsPrincipal)
FindAll(String)

檢索所有或具有指定主張類型的權利要求。

(繼承來源 ClaimsPrincipal)
FindFirst(Predicate<Claim>)

擷取第一個與指定謂詞匹配的主張。

(繼承來源 ClaimsPrincipal)
FindFirst(String)

取得具有指定權利要求類型的第一個權利要求。

(繼承來源 ClaimsPrincipal)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

將 中 SerializationInfo 填充以序列化當前 ClaimsPrincipal 物件所需的資料。

(繼承來源 ClaimsPrincipal)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
HasClaim(Predicate<Claim>)

判斷與該主張主體相關的任何主張身份包含與指定謂詞匹配的主張。

(繼承來源 ClaimsPrincipal)
HasClaim(String, String)

判定與該理賠主體相關的任何理賠身份是否包含指定類型與價值的理賠。

(繼承來源 ClaimsPrincipal)
IsInRole(Int32)

判斷目前主體是否屬於指定相對識別碼(RID)的 Windows 使用者群組。

IsInRole(SecurityIdentifier)

判斷目前的主體是否屬於具有指定安全識別碼(SID)的 Windows 使用者群組。

IsInRole(String)

判斷目前的主體是否屬於指定名稱的 Windows 使用者群組。

IsInRole(WindowsBuiltInRole)

判斷目前主體是否屬於指定的WindowsBuiltInRole 的Windows使用者群組。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
WriteTo(BinaryWriter, Byte[])

使用 BinaryWriter. 序列化

(繼承來源 ClaimsPrincipal)
WriteTo(BinaryWriter)

使用 BinaryWriter. 序列化

(繼承來源 ClaimsPrincipal)

適用於