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"?
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.
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.
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();
}
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()?
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.
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?
Re: Simple DistinguishedName question.
please give me any proper solution of this problemem,
thanks in advance
_____________________
Rakeback