आप .NET 3.5 या ऊपर पर कर रहे हैं, आप नए System.DirectoryServices.AccountManagement
(S.DS.AM) नाम स्थान जो की तुलना में इस एक बहुत आसान बना देता है का उपयोग कर सकते हैं यह हुआ करता था।
सभी इसे यहाँ के बारे में पढ़ें:
Managing Directory Security Principals in the .NET Framework 3.5
अद्यतन: बड़े MSDN पत्रिका के लेख अब ऑनलाइन नहीं हैं, दुर्भाग्य से - आप download the CHM for the January 2008 MSDN magazine करने के लिए Microsoft से और आवश्यकता होगी वहाँ में लेख पढ़ें।
मूल रूप से, आप एक "प्रमुख संदर्भ" (आम तौर पर अपने डोमेन), एक उपयोगकर्ता प्रिंसिपल की आवश्यकता है, और फिर आप अपने समूहों को बहुत आसानी से मिल:
public List<GroupPrincipal> GetGroups(string userName)
{
List<GroupPrincipal> result = new List<GroupPrincipal>();
// establish domain context
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);
// if found - grab its groups
if(user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
// iterate over all groups
foreach(Principal p in groups)
{
// make sure to add only group principals
if(p is GroupPrincipal)
{
result.Add((GroupPrincipal)p);
}
}
}
return result;
}
और कहा कि सभी वहाँ है! अब आपके पास प्राधिकरण समूहों का परिणाम (एक सूची) है जो उपयोगकर्ता से संबंधित है - उन पर पुनरावृत्ति करें, उनके नाम मुद्रित करें या जो कुछ भी आपको करना है।
अद्यतन: नहीं लगता है चाहिए: कुछ गुण है, जो UserPrincipal
वस्तु पर सामने नहीं कर रहे हैं का उपयोग करने के लिए, आप अंतर्निहित DirectoryEntry
में खुदाई करने के लिए की जरूरत है:
public string GetDepartment(Principal principal)
{
string result = string.Empty;
DirectoryEntry de = (principal.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{
if (de.Properties.Contains("department"))
{
result = de.Properties["department"][0].ToString();
}
}
return result;
}
अद्यतन # 2 भी कोड के इन दो के टुकड़े को एक साथ रखा करने के लिए कठिन .... लेकिन ठीक हो - यहाँ यह जाता है:
public string GetDepartment(string username)
{
string result = string.Empty;
// if you do repeated domain access, you might want to do this *once* outside this method,
// and pass it in as a second parameter!
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
// find the user
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);
// if user is found
if(user != null)
{
// get DirectoryEntry underlying it
DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{
if (de.Properties.Contains("department"))
{
result = de.Properties["department"][0].ToString();
}
}
}
return result;
}
@ टासिस्टो: दुर्भाग्यवश, वह संपत्ति सीधे 'उपयोगकर्ता प्रिंसिपल' पर उपलब्ध नहीं है - इसे प्राप्त करने के लिए मेरे अपडेट किए गए उत्तर को देखें। –
मुझे अपने विभाग-क्षेत्र – SamekaTV
@Tassito का मूल्य प्राप्त करने के लिए उपयोगकर्ता नाम देना होगा: ठीक है तो 1) डोमेन संदर्भ बनाएं, 2) उस उपयोगकर्ता को इसके नाम से ढूंढें, और 3) अपने विभाग को प्राप्त करने के लिए मेरे कोड स्निपेट का उपयोग करें –