I need to check users in the company using only their username and not password.
So I need a method like this
public bool UserExists(string username) { ... }
I know the System.DirectoryServices
namespace, but I donβt know where to start.
Any ideas?
There are 80,000+ entries, so try not to forget about it.
Thanks.
Edit:
I did this - my code:
private bool UserExists(string userName, string domain) { try { DirectoryEntry.Exists("WinNT://" + domain + ".[hidden].com/" + userName); return true; } catch (COMException) { return false; } }
I don't know if this is correct, but it seems to be working so far.
Michael answers in two important parts:
Update # 2:
I really used this:
public static bool LoggedOnUserExists() { var domain = new PrincipalContext(ContextType.Domain); UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, Environment.UserName); return foundUser != null; }
source share