Using System.Management to get the User, Domain and SID

I have seen a few examples of VBS scripts used to get the User ID, Domain and SID of the current user.
This information is valuable when making registry modifications for the current user programatically.
I needed a way to retrieve that information in a .NET module to include in other .NET programs.
Here is my adaptation:

using System.Management; //Be sure to add this as a Reference, also.

namespace UserDomainSid
{
   /// <summary>
   /// Object returned by the call to CUserDomainSid.GetUDS()
   /// </summary>
   public class CUdsObj
   {
      /// <summary>
      /// Domain prefix for the user
      /// </summary>
      public string strDomain
      {
         get;
         set;
      }

      /// <summary>
      /// User ID
      /// </summary>
      public string strUser
      {
         get;
         set;
      }

      /// <summary>
      /// SID for the user
      /// </summary>
      public string strSID
      {
         get;
         set;
      }

      /// <summary>
      /// Constructor
      /// </summary>
      public CUdsObj()
      {
         strDomain = "";
         strUser = "";
         strSID = "";
      }
   }
   /// <summary>
   /// Extracts the User, Domain and SID of the current user.
   /// </summary>
   public class CUserDomainSid
   {
      /// <summary>
      /// The root scope of the query.
      /// </summary>
      private static string strScope = @"\\.\root\cimv2";
      /// <summary>
      /// Use the System.Management namespace to get the current
      /// User, Domain and SID.
      /// This is useful for querying and setting certain values
      /// in the system registry.
      /// </summary>
      /// <returns>CUserDomainSid</returns>
      public static CUdsObj GetUDS()
      {
         CUdsObj udsRetVal = new CUdsObj();
         //
         // UserName will contain the domain and the user
         string strQuery = "Select UserName FROM Win32_ComputerSystem";

         ///////////////////////////////////////////////////////
         // Query the ManagementObject to find the current user
         using (ManagementObjectSearcher mosItems =
            new ManagementObjectSearcher(strScope, strQuery))
         {
            foreach (ManagementBaseObject mbo in mosItems.Get())
            {  //ManagementObjectCollection
               string[] arr_strUserDom =

                                                  mbo["UserName"].ToString().Split('\\');

               udsRetVal.strDomain = arr_strUserDom[0];
               udsRetVal.strUser = arr_strUserDom[1];
            }
         }

         udsRetVal.strSID =
             GetSid(udsRetVal.strDomain, udsRetVal.strUser);
        
         return udsRetVal;
      }

      /// <summary>
      /// GetSid uses the System.Management namespace to obtain the SID
      /// for a given domain and user name
      /// </summary>
      /// <param name="strDomain">the user's domain</param>
      /// <param name="strUser">the user's login ID</param>
      /// <returns>string (SID)</returns>
      public static string GetSid(string strDomain, string strUser)
      {
         string strRetValSid = "";
         ///////////////////////////////////////////////////////////////
         // Create a new query into the Win32_UserAccount to get the SID
         #region QUERY_REGION
         string strQuery =
            string.Format(
            // Go directly to the SID bypassing all others
               "Select SID " +
               "FROM Win32_UserAccount " +
               "WHERE" +
               " Domain='{0:G}'" +
               " AND Name='{1:G}'",
               strDomain, strUser);
         #endregion QUERY_REGION

         /////////////////////////////////////////////////////////////
         // Run the second query once we have the user name and Domain
         using (ManagementObjectSearcher mosItems =
            new ManagementObjectSearcher(strScope, strQuery))
         {  //ManagementObjectCollection
            foreach (ManagementBaseObject mbo in mosItems.Get())
            {  // Go directly to the SID bypassing all others
               strRetValSid = mbo["SID"].ToString();
            }
         }

         return strRetValSid;
      }
   }
}



 

posted @ Thursday, January 7, 2010 4:22 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview: