我想从用户那里获取Active Directory属性,我想使用System.DirectoryServices.AccountManagement
。
我的代码:
public static void GetUserProperties(string dc,string user)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);
string firstname = u.GivenName;
string lastname = u.Surname;
string email = u.EmailAddress;
string telephone = u.VoiceTelephoneNumber;
...//how I can get company and other properties?
}
答案 0 :(得分:10)
您可以转换到DirectoryServices命名空间以获取所需的任何属性。
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);
string firstname = u.GivenName;
string lastname = u.Surname;
string email = u.EmailAddress;
string telephone = u.VoiceTelephoneNumber;
string company = String.Empty;
...//how I can get company and other properties?
if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
// Transition to directory entry to get other properties
using (var entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject())
{
if (entry.Properties["company"] != null)
company = entry.Properties["company"].Value.ToString();
}
}
答案 1 :(得分:0)
如果要更改属性,请不要忘记在更改值后调用userPrincipal.save()。
entry.Properties["company"].value = company;
userPrincipal.save();