WindowsAccountFacade

.NET Framework 4.5 Documentation

Class WindowsAccountFacade

System.Object  ›  System.Security.Principal  ›  WindowsAccountFacade

Represents a Windows account facade that abstracts the details of a Windows account.

This class provides a consistent way to interact with Windows principal objects, regardless of their underlying representation.

Syntax

public class WindowsAccountFacade : WindowsIdentity

Namespace: System.Security.Principal

Assembly: System.Security.Principal.Windows (in System.Security.Principal.Windows.dll)

Constructors

WindowsAccountFacade(IntPtr userToken)

Initializes a new instance of the WindowsAccountFacade class with the specified user token.

public WindowsAccountFacade(IntPtr userToken);

WindowsAccountFacade(string sid)

Initializes a new instance of the WindowsAccountFacade class with the specified security identifier (SID).

public WindowsAccountFacade(string sid);

Properties

Name Description
AuthenticationType Gets the type of authentication used for this identity.
IsAuthenticated Gets a value indicating whether the user has been authenticated.
IsGuest Gets a value indicating whether the current user is a guest.
IsSystem Gets a value indicating whether the current user is a system account.
IsAnonymous Gets a value indicating whether the current user is an anonymous user.
Name Gets the name of the current principal.
Owner Gets the owner of the security identifier.
Token Gets the handle to the Windows access token for the identity.

Methods

Name Description
IsInRole(string role) Determines whether the current principal is a member of the specified role.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
ToString() Returns a string that represents the current object.

Example

The following example demonstrates how to create and use a WindowsAccountFacade to check if a user is in a specific role.

using System;
using System.Security.Principal;

public class Example
{
    public static void Main(string[] args)
    {
        // Assuming you have a valid user token or SID
        // For demonstration, let's create a simple facade from a SID string
        string userSid = "S-1-5-18"; // Well-known SID for Local System

        try
        {
            WindowsAccountFacade facade = new WindowsAccountFacade(userSid);

            Console.WriteLine($"User Name: {facade.Name}");
            Console.WriteLine($"Authentication Type: {facade.AuthenticationType}");
            Console.WriteLine($"Is Authenticated: {facade.IsAuthenticated}");
            Console.WriteLine($"Is System: {facade.IsSystem}");

            // Example of checking role (this will likely be false for Local System)
            if (facade.IsInRole("Administrators"))
            {
                Console.WriteLine($"{facade.Name} is in the Administrators role.");
            }
            else
            {
                Console.WriteLine($"{facade.Name} is NOT in the Administrators role.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Remarks

The WindowsAccountFacade class is designed to simplify the process of working with Windows security principals. It inherits from WindowsIdentity, providing access to standard Windows identity properties and methods, while offering a facade pattern for potentially cleaner usage in certain scenarios.

When initializing with a user token (IntPtr), ensure the token is valid and the application has the necessary permissions to access it.