铸造ActiveDirectory中的pwdLastSet属性,而不使用ActiveDs [英] Casting ActiveDirectory pwdLastSet property without using ActiveDs

查看:1363
本文介绍了铸造ActiveDirectory中的pwdLastSet属性,而不使用ActiveDs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。所以我试图找到一种方法来避免,因为我遇到了麻烦的DLL在安装程序中露面,包括在我的项目ActiveDs。唯一的理由,是铸就的pwdLastSet为整型。



我发现了一个未公开的替代品。当测试它,它是死亡或429.49673秒关闭。我不知道为什么,没有任何人有什么想法? (我测试四十九分之二十零人429.49673秒关)



更新:它看起来像它发生时,LowPart为负<。 / p>

代码:

 私有静态字符串DateTest(){
返回DateTest(Environment.UserName);
}
私人静态字符串DateTest(用户名字符串){$ B $ 2.用户名= userName.Trim();
日期时间砍死,正常;使用
(DS的DirectorySearcher =新的DirectorySearcher()){
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Add(distinguishedName来);
ds.PropertiesToLoad.Add(的pwdLastSet);
ds.PageSize = 1;
ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
ds.Filter =的String.Format((及(objectCategory属性=用户)(sAMAccountName赋= {0})),用户名);

SearchResult中SR = ds.FindOne();
砍死= DateTime.FromFileTime((长)sr.Properties [的pwdLastSet] [0]);使用(的DirectoryEntry用户= sr.GetDirectoryEntry())

{
VAR值= user.Properties [的pwdLastSet] [0]作为ActiveDs.LargeInteger;
VAR的longValue =(((长)value.HighPart)LT;< 32)+(长)value.LowPart;
=正常DateTime.FromFileTime(的longValue);
}
}

返回的String.Format({3} - 差:{0:0.0}秒建立的方法返回:{1}黑客攻击方法的返回值:{。 2},
hacked.Subtract(正常).TotalSeconds,正常,黑客入侵,用户名);
}
}



参考文献:




  • 活动DS类型库

  • 的System.DirectoryServices


解决方案

您将需要翻译的AD长整型这样,你应该不需要再ActiveDs:

 长的pwdLastSet = CovertADSLargeIntegerToInt64(oUser.Properties [的pwdLastSet]值。); 

公共静态的Int64 ConvertADSLargeIntegerToInt64(对象adsLargeInteger)
{
VAR highPart =(Int32)已adsLargeInteger.GetType()。InvokeMember(HighPart,System.Reflection.BindingFlags.GetProperty ,空,adsLargeInteger,NULL);
VAR lowPart =(Int32)已adsLargeInteger.GetType()InvokeMember(LowPart,System.Reflection.BindingFlags.GetProperty,空,adsLargeInteger,NULL);
返回highPart *((的Int64)UInt32.MaxValue + 1)+ lowPart;
}


Ok. So I'm trying to find a way to avoid including ActiveDs in my project because I'm having trouble getting the dll to show up in the installer. The only reason to have it is to cast the pwdLastSet as a LongInteger.

I found an undocumented alternative. When testing it, it is either dead on or 429.49673 seconds off. I'm not sure why, does anybody have any idea? (20/49 people that i tested are 429.49673 seconds off).

UPDATE: It looks like it happens when the LowPart is negative.

Code:

    private static string DateTest() {
        return DateTest(Environment.UserName);
    }
    private static string DateTest(string userName) {
        userName = userName.Trim();
        DateTime hacked, normal;
        using (DirectorySearcher ds = new DirectorySearcher()) {
            ds.SearchScope = SearchScope.Subtree;
            ds.PropertiesToLoad.Add("distinguishedName");
            ds.PropertiesToLoad.Add("pwdLastSet");
            ds.PageSize = 1;
            ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
            ds.Filter = string.Format("(&(objectCategory=user)(sAMAccountName={0}))", userName);

            SearchResult sr = ds.FindOne();
            hacked = DateTime.FromFileTime((long)sr.Properties["pwdLastSet"][0]);

            using (DirectoryEntry user = sr.GetDirectoryEntry()) {
                var value = user.Properties["pwdLastSet"][0] as ActiveDs.LargeInteger;
                var longValue = (((long)value.HighPart) << 32) + (long)value.LowPart;
                normal = DateTime.FromFileTime(longValue);
            }
        }

        return string.Format("{3} - Difference: {0:0.0} seconds. Established Method returns: {1}. Hacked method returns: {2}",
            hacked.Subtract(normal).TotalSeconds, normal, hacked, userName);
    }
}

References:

  • Active DS Type Library
  • System.DirectoryServices

解决方案

You'll need to translate the AD Long Integer like this and you shouldn't need ActiveDs anymore:

long pwdLastSet = CovertADSLargeIntegerToInt64(oUser.Properties["pwdLastSet"].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;
}

这篇关于铸造ActiveDirectory中的pwdLastSet属性,而不使用ActiveDs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆