CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    9

    Simple DistinguishedName question.

    I need the DistinguishedName of a user account on a Windows XP system. The computer does not belong to a domain, but a workgroup, instead. What would be the DN if the following were true?:

    user name: brandon
    workgroup: WORKGROUP
    computer name: brandoncomp

    Would it be?: CN=brandon, OU=USERS, DC=brandoncomp

    And (I know this doesn't really pertain to this particular forum, but) how would I set that up in C# .NET, set set a string = "CN=brandon, OU=USERS, DC=brandoncomp"?

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Simple DistinguishedName question.

    DistinguishedName is a directory property, not an nt username property. You cannot query for a DistinguishedName on a non-domain account.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Jan 2010
    Posts
    9

    Re: Simple DistinguishedName question.

    Thank you for the answer... I forgot that I posted this already. What I was looking for was this (in case this helps someone) This is C# .NET, btw:

    Code:
    "WinNT://" + Environment.MachineName + "/userName"
    pretty simplistic.



    But, I would like to know one other thing. How can I tell if the user account exists? Right now I can see if the .Name == userName, but that doesn't seem like a good method, plus, if you try to call .Name and the user didn't exist, a COMException is thrown.

    here's my code:
    Code:
    DirectoryEntry user = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + args[0]);
    
    if (user.Name != args[0])
        // Error handling code here.

  4. #4
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Re: Simple DistinguishedName question.

    You'll have to use WMI.

    To get you started: Here is a function that will get a list of all the users. All you have to do is compare the list to the username in question, and then you'll know if it exists.

    Code:
    public static void GetUsers()   
    {
    
        SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='ComputerNameHere'");   
      
         try   
         {   
                ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);   
      
                Console.WriteLine("User Accounts\n");   
      
                foreach (ManagementObject mObject in mSearcher.Get())   
                {   
                    Console.WriteLine(mObject["Name"]);   
    //Check if user name matches here!
                }   
          }   
          catch (Exception ex)   
          {   
                Console.WriteLine(ex.ToString());   
          }   
      
         Console.ReadKey();   
     }
    Last edited by Craig Gemmill; January 13th, 2010 at 12:28 AM.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  5. #5
    Join Date
    Jan 2010
    Posts
    9

    Re: Simple DistinguishedName question.

    Thank you very much. =)

    Minor question: The line
    Code:
    Console.WriteLine(mObject["Name"]);
    writes just fine to the console, but when I try:
    Code:
    if (mObject["Name"].ToString() == userName)
    it needs the .ToString() so the types are identical... Why can it write to the console with or without the .ToString()?

  6. #6
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Simple DistinguishedName question.

    ToString() is called by the .NET Framework implicitly when printing any object as a string, since every .NET Object supports the ToString() method.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  7. #7
    Join Date
    Mar 2011
    Posts
    3

    Re: Simple DistinguishedName question.

    i am also facing the same problem of user account on a Windows XP system, but don,t undersatnd but can i do? can you tell me more about this and how can i implement it?

  8. #8
    Join Date
    Mar 2011
    Posts
    3

    Re: Simple DistinguishedName question.

    please give me any proper solution of this problemem,
    thanks in advance
    _____________________
    Rakeback

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured