Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

active directory - How to read "uSNChanged" property using C#

I want to get the last modified or created attributes via the uSNChanged value in ActiveDirectory using C# ... I was also trying to find the max value of uSNChanged, can you help me to find out the solution? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways to retrieve the uSNChanged property via .NET:

  1. Include a reference to a COM library: "Active DS Type Library", then you need to use the IADsLargeInterger to retrieve the value and finally convert it to a long. For example:

    IADsLargeInteger li_ad = (IADsLargeInteger)oUser.Properties["USNChanged"].Value;
    long l_uChanged = GetLongFromLargeInteger( li_ad );
    
    static long GetLongFromLargeInteger(  IADsLargeInteger  Li )
    {
        long retval = Li.HighPart;
        retval <<=32;
        retval |=(uint)Li.LowPart;
        return retval;
    }
    
  2. Translate the values only using C#. Thanks to Simon Gilbee, we have this option:

     long usnChanged = ConvertADSLargeIntegerToInt64(oUser.Properties["USNChanged"].Value);
    
     public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
     {
       var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
       var lowPart  = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart",  System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
       return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
     }
    

I highly recommend you go with Option #2 to avoid problems with the legacy ActiveDs library and won't need answers off this list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...