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 重载允许使用不同的角色上下文来检查用户角色。

构造函数

WindowsPrincipal(WindowsIdentity)

使用指定的 WindowsIdentity 对象初始化 WindowsPrincipal 类的新实例。

属性

Claims

获取包含所有声明的集合,这些声明来自于与此声明主体关联的所有声明标识符。

(继承自 ClaimsPrincipal)
CustomSerializationData

包含派生类型提供的任何其他数据。 通常在调用 WriteTo(BinaryWriter, Byte[]) 时设置。

(继承自 ClaimsPrincipal)
DeviceClaims

从此主体中获取所有 Windows 设备声明。

Identities

获取一个集合,该集合包含与此声明主体关联的所有声明标识。

(继承自 ClaimsPrincipal)
Identity

获取当前用户的标识。

UserClaims

从此主体中获取所有 Windows 用户声明。

方法

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)

适用于