我试过了
DirectoryEntry user = new DirectoryEntry(iMem);var AccountExpiration = DateTime.FromfileTime((int)user.PropertIEs["accountExpires"].Value);
它不起作用,只给我错误“指定的演员表无效”.
我用的时候
var AccountExpiration = user.PropertIEs["accountExpires"];
返回一个我无法读取的com对象.
使用windows powershell,工作正常,我不知道为什么这不会工作…
这是我在powershell中使用的代码
$Expires = [datetime]::FromfileTime($tmpuser.accountExpires)解决方法 您可以使用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,请执行以下 *** 作:
//assume 'user' is DirectoryEntry representing user to checkDateTime 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值的另一种方法是使用反射:
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); 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);}总结
以上是内存溢出为你收集整理的c# – 从ActiveDirectory检索用户帐户过期全部内容,希望文章能够帮你解决c# – 从ActiveDirectory检索用户帐户过期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)