2013-03-25 4 views
6

सक्रिय निर्देशिका (उपयोगकर्ता) में उपयोगकर्ता को देखने के लिए System.DirectoryServices.AccountManagement नामस्थान का उपयोग शुरू किया गया। मुझे उपयोगकर्ता के प्रबंधक की भी आवश्यकता है, लेकिन मुझे लगता है कि इस नामस्थान का उपयोग करके सड़क में टक्कर लगी है।सी # - सक्रिय निर्देशिका में उपयोगकर्ता प्रबंधक को देखने के लिए

class Person { 
    // Fields 
    public string GivenName = null; 
    public string Surname = null; 
    public string DistinguishedName = null; 
    public string Email = null; 
    public string MangerDistinguishedName = null; // Unable to set this 

    // Constructor 
    public Person(string userName) { 
     UserPrincipal user = null; 

     try { 
      user = GetUser(userName); 

      if (user != null) { 
       this.GivenName = user.GivenName; 
       this.Surname = user.Surname; 
       this.DistinguishedName = user.DistinguishedName; 
       this.Email = user.EmailAddress; 
       this.MangerDistinguishedName = user.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME> 
      } 
      else { 
       throw new MissingPersonException("Person not found"); 
      } 
     } 
     catch (MissingPersonException ex) { 
      MessageBox.Show(
       ex.Message 
       , ex.reason 
       , MessageBoxButtons.OK 
       , MessageBoxIcon.Error 
      ); 
     } 
     catch (Exception ex) { 
      MessageBox.Show(
       ex.Message 
       , "Error: Possible connection failure, or permissions failure to search for the username provided." 
       , MessageBoxButtons.OK 
       , MessageBoxIcon.Error 
      ); 
     } 
     finally { 
      user.Dispose(); 
     } 
    } 

व्यक्ति

private UserPrincipal GetUser(string userName) { 
     PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
     UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName); 

     return user; 
    } 

सीधे एक विशेष उपयोगकर्ता के प्रबंधक की विशिष्ट नाम का उपयोग करने के लिए एक और तरीका क्या है के लिए खोज निष्पादित करें: वर्तमान कोड एक व्यक्ति पाने के लिए?

    VB में
  • संभव आंशिक जवाब here, लेकिन मैं प्रबंधकों की चर्चा करते हुए बारे में कुछ नहीं देखते हैं।
  • एक और संभावित आंशिक एक here, फिर से, प्रबंधकों के बारे में कुछ भी नहीं।

उत्तर

7

आप .NET 3.5 और ऊपर और System.DirectoryServices.AccountManagement (S.DS.AM) नाम स्थान का उपयोग कर पर हैं, तो आप आसानी से की तरह Manager आदि

, और अधिक उन्नत गुण पर प्राप्त करने के लिए मौजूदा UserPrincipal वर्ग का विस्तार कर सकते

यहाँ यह सब के बारे में पढ़ें:

,210

असल में, तुम सिर्फ UserPrincipal के आधार पर एक व्युत्पन्न वर्ग को परिभाषित, और फिर आप आप चाहते हैं कि आपके अतिरिक्त गुणों को परिभाषित:

[DirectoryRdnPrefix("CN")] 
[DirectoryObjectClass("Person")] 
public class UserPrincipalEx : UserPrincipal 
{ 
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context) 
    { } 

    // Implement the constructor with initialization parameters.  
    public UserPrincipalEx(PrincipalContext context, 
         string samAccountName, 
         string password, 
         bool enabled) : base(context, samAccountName, password, enabled) 
    {} 

    // Create the "Department" property.  
    [DirectoryProperty("department")] 
    public string Department 
    { 
     get 
     { 
      if (ExtensionGet("department").Length != 1) 
       return string.Empty; 

      return (string)ExtensionGet("department")[0]; 
     } 
     set { ExtensionSet("department", value); } 
    } 

    // Create the "Manager" property.  
    [DirectoryProperty("manager")] 
    public string Manager 
    { 
     get 
     { 
      if (ExtensionGet("manager").Length != 1) 
       return string.Empty; 

      return (string)ExtensionGet("manager")[0]; 
     } 
     set { ExtensionSet("manager", value); } 
    } 

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue) 
    { 
     return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue); 
    } 

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue) 
    { 
     return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue); 
    } 
} 

अब, आप अपने कोड में UserPrincipalEx की "विस्तारित" संस्करण का उपयोग कर सकते हैं:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser"); 

    // you can easily access the Manager or Department now 
    string department = inetPerson.Department; 
    string manager = inetPerson.Manager; 
}   
+0

क्या आपने यह कोशिश की है? यह मेरे लिए काम नहीं करता है। UserPrincipalEx.FindByIdentity UserPrincipalEx ऑब्जेक्ट को वापस नहीं करता है, और UserPrincipalEx को कास्टिंग एक अवैधCastException का कारण बनता है। – Naikrovek

+1

@Nikikrovek: क्षमा करें - मेरी गलती - मैंने अपने (बहुत लंबा) नमूना से थोड़ा अधिक कोड काट दिया था। मैंने दो अधिभारित 'FindByIdentity' और' FindByIdentityWithType' विधियों को याद किया था - मैंने उनको अपने कोड स्निपेट में जोड़ा - और हाँ, इस कोड के साथ, मैंने इसे Win Server 2008 R2 सक्रिय निर्देशिका के विरुद्ध चेक किया और यह मेरे लिए ठीक काम करता है। –

+0

अच्छा काम करता है, धन्यवाद। – Naikrovek

संबंधित मुद्दे