我知道之前曾问过这类问题,但其他方法现在都让我失望了.
正如我们的Windows服务轮询AD,给定一个LDAP(即LDAP://10.32.16.80)和该AD服务器中要搜索的用户组列表.它检索这些给定组中的所有用户,以递归方式搜索这些组以获取更多组.然后将每个用户添加到另一个应用程序认证用户列表中.
这部分应用程序运行成功.但是,我们需要每个用户的友好域名(即他们登录DOMAIN /用户名的一部分)
因此,如果有一个用户属于TEST域,名为Steve:TEST/steve就是他的登录名.我能够在广告中找到史蒂夫,但是我还需要将"TEST"与他的AD信息一起存储.
再一次,通过使用目录搜索器和我给出的LDAP IP,我可以找到'史蒂夫',但是考虑到LDAP IP,我如何才能找到友好的域名?
当我尝试以下代码时,我在尝试访问'defaultNamingContext'时遇到错误:
System.Runtime.InteropServices.COMException(0x8007202A):身份验证机制未知.
这是代码:
private string SetCurrentDomain(string server)
{
string result = string.Empty;
try
{
logger.Debug("'SetCurrentDomain'; Instantiating rootDSE LDAP");
DirectoryEntry ldapRoot = new DirectoryEntry(server + "/rootDSE", username, password);
logger.Debug("'SetCurrentDomain'; Successfully instantiated rootDSE LDAP");
logger.Debug("Attempting to retrieve 'defaultNamingContext'...");
string domain = (string)ldapRoot.Properties["defaultNamingContext"][0]; //THIS IS WHERE I HIT THE COMEXCEPTION
logger.Debug("Retrieved 'defaultNamingContext': " + domain);
if (!domain.IsEmpty())
{
logger.Debug("'SetCurrentDomain'; Instantiating partitions/configuration LDAP entry");
DirectoryEntry parts = new DirectoryEntry(server + "/CN=Partitions,CN=Configuration," + domain, username, password);
logger.Debug("'SetCurrentDomain'; …Run Code Online (Sandbox Code Playgroud) 我正在使用这些System.DirectoryServices.ActiveDirectory类来查找所有Active Directory用户.代码很简单:
var context = new PrincipalContext(ContextType.Domain);
var searcher = new PrincipalSearcher(new UserPrincipal(context));
var results = searcher.FindAll();
Run Code Online (Sandbox Code Playgroud)
我希望以"友好"(又称"Windows 2000之前"格式)获得域限定用户名,例如."CONTOSO\SmithJ".UserPrincipal.SamAccountName给我用户名部分,但我如何获得域名部分?我不能假设域将与机器或当前用户的域相同.
我有以下代码返回UserPrincipal但loginname从不包含域名.也没有"域名"或类似的属性.
如何从UserPrincipal或PrincipalSearcher域获取用户/返回的用户?
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = new UserPrincipal(ctx);
user.SamAccountName = txtSearch.Text;
PrincipalSearcher searcher = new PrincipalSearcher(user);
PrincipalSearchResult<Principal> results = searcher.FindAll();
foreach (UserPrincipal u in results)
{
Response.Write(u.Name + "<br />");
}
Run Code Online (Sandbox Code Playgroud) 我需要按以下格式列出特定本地组中的所有用户:"Domain\UserName".我可以为该组提取GroupPrincipal对象的集合,但我不知道如何以所需格式获取用户.GroupPrincipal没有属性Domain.
以下代码输出没有域的用户(例如"UserName").
using (var context = new PrincipalContext(ContextType.Machine, null))
{
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, @"My Local Group"))
{
if (group != null)
{
foreach (var p in group.GetMembers(false))
{
Console.WriteLine(p.SamAccountName);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以从主要对象获取域netbios名称?如果是这样,如何获得它?