Skip to content

Instantly share code, notes, and snippets.

@beliy26
Created December 11, 2014 11:00
Show Gist options
  • Save beliy26/9e4a9e06ffec81da382e to your computer and use it in GitHub Desktop.
Save beliy26/9e4a9e06ffec81da382e to your computer and use it in GitHub Desktop.
DomainUser class
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