开发者

How i can get windows logon domain name from AD domain name

I have AD domain name like MyDomain.com and Windows logon domain name like MD (MD=MyDomain). How I can get win logon domain name from AD domain using DirectoryEn开发者_运维百科try.Properties collection?


Probably, you could use this:

string username = "<username>";

DirectoryEntry de = new DirectoryEntry(
    "LDAP://" + ConfigurationManager.AppSettings["ADDomain"],
    ConfigurationManager.AppSettings["ADUsername"],
    ConfigurationManager.AppSettings["ADPassword"]);

DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = string.Format("samaccountname={0}",
    (username.Split('\\').Length > 1) ? username.Split('\\')[1] : username);

SearchResult result = ds.FindOne();
if (result == null)
    throw new ArgumentException(
        string.Format("Username '{0}' does not exist in the active directory", username), "username");

You can then use the Properties collection on the SearchResult object to get information from the user object (e.g. result.Properties["samaccountname"]). Some useful keys are:

  • List item samaccountname (Windows username)
  • List item displayName (Full name)
  • List item telephoneNumber
  • List item mail (email address)
  • List item department (the department the user belongs to)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜