locked
Get sAMAccount name in form of domain\userId RRS feed

  • Question

  • Hi,

    I want to fetch the sAMAccount name in the form of "domain\userId" from the Active Directory. How can I fetch this information. I have used many classes but couldnt found any solution.

    Please provide me the solution.

    Thanks and Regards

    Harsh

    Tuesday, January 18, 2011 10:10 AM

Answers

  • Hi,

    I got the solution,

    Reference : http://blog.dotsmart.net/2009/03/11/getting-a-machine%E2%80%99s-netbios-domain-name-in-csharp/

    1. Fetch the "distinguishedName" from the AD and create the domainController name as "ABC.XYZ.COM" from the distinguishedName property (CN="users",CN="userName",DC"ABC",DC="XYZ",DC="COM")

    2. Now create one class and use the above refernced code and pass this domainController name to the function (this is a paramaterised function of the above code reference link.

      public static string GetMachineNetBiosDomain(string domainControllerName)
        {
            IntPtr pBuffer = IntPtr.Zero;

            WKSTA_INFO_100 info;
            int retval = NetWkstaGetInfo(domainControllerName, 100, out pBuffer);
            if (retval != 0)
                throw new Win32Exception(retval);

            info = (WKSTA_INFO_100)Marshal.PtrToStructure(pBuffer, typeof(WKSTA_INFO_100));
            string domainName = info.wki100_langroup;
            NetApiBufferFree(pBuffer);
            return domainName;
        }

    This will give the domainName of the user and concatenate the domainName with the sAMAccount name. Final result will be in the form of "domain\userId".

    Thanks and Regards

    Harsh

    • Edited by KapoorHarsh Wednesday, January 19, 2011 7:34 AM Bold the function
    • Marked as answer by KapoorHarsh Wednesday, January 19, 2011 7:35 AM
    Wednesday, January 19, 2011 7:33 AM