Bu mümkün mü? Kod örneği iyi olurdu. Bölüm ve şirket özellikleri kullanıcı için mevcutsa, bunun gibi bir şey yapmalıdır.Bir UserPrincipal nesnesi verilen Active Directory'den "Şirket" ve "Bölüm" nasıl alınır?
cevap
Aslında soruydu. Bir extension method ile bunun nasıl İşte
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace MyExtensions
{
public static class AccountManagementExtensions
{
public static String GetProperty(this Principal principal, String property)
{
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(property))
return directoryEntry.Properties[property].Value.ToString();
else
return String.Empty;
}
public static String GetCompany(this Principal principal)
{
return principal.GetProperty("company");
}
public static String GetDepartment(this Principal principal)
{
return principal.GetProperty("department");
}
}
}
Yukarıdaki kod çoğu durumda çalışacak (yani standart Metin için çalışacak olan/Dize Tek Değer, Active Directory nitelikleri). Kodu değiştirmek ve ortamınız için daha fazla hata işleme kodu eklemeniz gerekir.
Sen projenize "Uzatma Class" add bunu kullanmak ve daha sonra bunu yapabilirsiniz:
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, "youruser");
Console.WriteLine(userPrincipal.GetCompany());
Console.WriteLine(userPrincipal.GetDepartment());
Console.WriteLine(userPrincipal.GetProperty("userAccountControl"));
(BTW, bu Uzatma Özellikleri için büyük bir kullanım olurdu - too bad it won't be in C# 4 either.)
Bir .NET 3.5 (System.DirectoryServices.AccountManagement.)UserPrincipal
için özelliklerinin ikisini almak için bir userPrincipalName
verilmemesi -Nesne nasıl
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://dnsNameOfYourDC.my.company.com";
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.PropertiesToLoad.Add("department");
deSearch.PropertiesToLoad.Add("company");
deSearch.SearchScope = SearchScope.Subtree;
deSearch.Filter = "(&(objectClass=User)(userPrincipalName=MyPrincipalName))";
SearchResultCollection results = deSearch.FindAll():
foreach (SearchResult result in results)
{
ResultPropertyCollection props = result.Properties;
foreach (string propName in props.PropertyNames)
{
//Loop properties and pick out company,department
string tmp = (string)props[propName][0];
}
}
bir küçük nitpick: LDAP filtresinde, objectClass yerine "objectCategory" kullanmayı tercih ederim. Niye ya? ObjectCategory tek değerlidir ve Active Directory'de dizine eklenmiştir, bu nedenle searcher, objectCategory kullanılarak daha hızlıdır. –
Aslında, Windows Server 2008 üzerindeyseniz, objectClass özniteliği varsayılan olarak endekslenir. Windows Server 2000 veya 2003'te değilsiniz. –
Bu harika bir çözümdür ve teşekkürler. Özellikle System.DirectoryServices.AccountManagement.UserPrincipal ile uğraşırken aşağıdaki gibi doğru işaretlemek zorunda kaldı. – wgpubs
Ayrıca, gereksinim duyduğunuz özelliklere sahip özel yöneticiler oluşturmak için Principal Extensions'a (http://msdn.microsoft.com/en-us/library/bb552835.aspx) bakabilirsiniz. –
Bir "Set" metodu yapmaktan ve bir değeri nasıl kurtaracaksınız? – JustinStolle
@PerNoalt Kod için teşekkürler, ancak doğrudan bir mülk edinmenin daha kısa bir yolu yok, örneğin: UserPrincipal departmanı doğrudan uzantıları kullanmadan mı? – Rama