Get Active Directory User’s GUID and SID in C#? (Part 1)

Get the Current User’s Active Directory info?

Here is how to get the currently logged in users Active Directory GUID and SID.

Step 1 – Create a new Console Application project in Visual Studio.

Step 2 – Add a .NET reference to System.DirectoryServices.AccountManagement.

Step 3 – Populate the Main Method as shown below.

using System;
using System.DirectoryServices.AccountManagement;

namespace GetAdUserInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Name: " + UserPrincipal.Current.Name);
            Console.WriteLine("User: " + UserPrincipal.Current.UserPrincipalName);
            Console.WriteLine("GUID: " + UserPrincipal.Current.Guid);
            Console.WriteLine(" SID: " + UserPrincipal.Current.Sid);
        }
    }
}

See Part 2 – Get a named User’s Active Directory info?

2 Comments

  1. BenL says:

    I'm looking for a way to obtain a "machine id" for every computer in the world. It needs to be unique (as possible). How stable, and how unique is the Current.Sid value?

    If it is not suitable as a unique machine identifier...is there a way to obtain one through a different method?

    Thanks for your help

  2. [...] we learned how to Get the Current User’s Active Directory info. Today we will learn how to get a named user’s Active Directory [...]

Leave a Reply

How to post code in comments?