2013-08-14 5 views
5

मैं खातों से समाप्ति तिथि पुनर्प्राप्त करने का प्रयास कर रहा हूं।सक्रिय निर्देशिका से उपयोगकर्ता खाता समाप्ति को पुनर्प्राप्त करना

मैं

DirectoryEntry user = new DirectoryEntry(iMem); 

var AccountExpiration = DateTime.FromFileTime((int)user.Properties["accountExpires"].Value); 

यह काम नहीं करता की कोशिश की है, केवल मुझे त्रुटि "निर्दिष्ट डाली मान्य नहीं है" देता है।

जब मैं का उपयोग

var AccountExpiration = user.Properties["accountExpires"]; 

रिटर्न एक कॉम वस्तु है, जो मैं पढ़ने में असमर्थ हूँ।

खिड़कियों powershell का उपयोग करना, ठीक काम करता है, मैं क्यों यह अभ्यस्त काम ...

यह है कोड मैं powershell में उपयोग

$Expires = [datetime]::FromFileTime($tmpUser.accountExpires) 

उत्तर

10

आप पूरा करने के लिए System.DirectoryServices.AccountManagement नाम स्थान का उपयोग कर सकते नहीं मिलता है ये कार्य। PrincipalContext से UserPrincipal प्राप्त करने के बाद, आप UserPrincipal.AccountExpirationDate संपत्ति का निरीक्षण कर सकते हैं।

PrincipalContext context = new PrincipalContext(ContextType.Domain); 

UserPrincipal p = UserPrincipal.FindByIdentity(context, "Domain\\User Name"); 

if (p.AccountExpirationDate.HasValue) 
{ 
    DateTime expiration = p.AccountExpirationDate.Value.ToLocalTime(); 
} 

यदि आप DirectoryEntry उपयोग करना चाहते हैं, ऐसा करते हैं:

private static long ConvertLargeIntegerToLong(object largeInteger) 
{ 
    Type type = largeInteger.GetType(); 

    int highPart = (int)type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null); 
    int lowPart = (int)type.InvokeMember("LowPart", BindingFlags.GetProperty | BindingFlags.Public, null, largeInteger, null); 

    return (long)highPart <<32 | (uint)lowPart; 
} 

object accountExpires = DirectoryEntryHelper.GetAdObjectProperty(directoryEntry, "accountExpires"); 
var asLong = ConvertLargeIntegerToLong(accountExpires); 

if (asLong == long.MaxValue || asLong <= 0 || DateTime.MaxValue.ToFileTime() <= asLong) 
{ 
    return DateTime.MaxValue; 
} 
else 
{ 
    return DateTime.FromFileTimeUtc(asLong); 
} 
:

//assume 'user' is DirectoryEntry representing user to check 
DateTime expires = DateTime.FromFileTime(GetInt64(user, "accountExpires")); 

private Int64 GetInt64(DirectoryEntry entry, string attr) 
{ 
    //we will use the marshaling behavior of the searcher 
    DirectorySearcher ds = new DirectorySearcher(
    entry, 
    String.Format("({0}=*)", attr), 
    new string[] { attr }, 
    SearchScope.Base 
    ); 

    SearchResult sr = ds.FindOne(); 

    if (sr != null) 
    { 
     if (sr.Properties.Contains(attr)) 
     { 
      return (Int64)sr.Properties[attr][0]; 
     } 
    } 

    return -1; 
} 

accountExpires मूल्य पार्स करने का एक और तरीका प्रतिबिंब उपयोग कर रहा है

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