Tas*_*sto 5 c# asp.net active-directory
我想从Active Directory检索用户的登录名.
例如,名称是'Jan Van der Linden'在将此名称作为参数后,我必须获取他的登录名作为回报,例如jvdlinden
由于您使用的是.NET 3.5及更高版本,因此您应该查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间.在这里阅读所有相关内容:
基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:
public string GetLoginName(string userName)
{
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find user by name
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
if(user != null)
return user.SamAccountName;
else
return string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
新的S.DS.AM使得在AD中使用用户和群组变得非常容易: