Created
December 11, 2014 11:00
-
-
Save beliy26/9e4a9e06ffec81da382e to your computer and use it in GitHub Desktop.
DomainUser class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DomainUser | |
{ | |
/// <summary> | |
/// Method return true if user is domain user or false if is not domain user. Method return null if program run as local user | |
/// </summary> | |
/// <param name="username"></param> | |
/// <param name="domainServerName"></param> | |
/// <returns></returns> | |
public static bool? IsDomainUser(string username, string domainServerName) | |
{ | |
if (!IsCurrentUserDoaminUser()) | |
{ | |
return null; | |
} | |
var entry = new DirectoryEntry(string.Concat("WinNT://", domainServerName)); | |
foreach (DirectoryEntry child in entry.Children) | |
{ | |
if (child.SchemaClassName != "User") continue; | |
if (String.Equals(child.Name, username, StringComparison.CurrentCultureIgnoreCase)) | |
return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// method return true if program run as domain user or false if run as local user | |
/// </summary> | |
/// <returns></returns> | |
private static bool IsCurrentUserDoaminUser() | |
{ | |
return Environment.MachineName != Environment.UserDomainName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment