Getting authenticate AD users objectGuid from asp.net

前端 未结 3 1621
名媛妹妹
名媛妹妹 2020-12-30 13:36

I am using windows authentication within an ASP.NET application. I am wondering how to best get the objectGuid from the currently logged in user?

Regards, Egil.

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 13:56

    You need to use NativeGuid property. C# code:

    string login = HttpContext.Current.User.Identity.Name;
    string domain = login.Substring(0, login.IndexOf('\\'));
    string userName = login.Substring(login.IndexOf('\\') + 1);
    DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + domain);
    DirectorySearcher searcher = new DirectorySearcher(domainEntry);
    searcher.Filter = string.Format(
       "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))",
       userName);
    SearchResult searchResult = searcher.FindOne();
    DirectoryEntry entry = searchResult.GetDirectoryEntry();
    Guid objectGuid = new Guid(entry.NativeGuid);
    

提交回复
热议问题