问题描述
我尝试从帐户到期日期。
我试过
的DirectoryEntry用户=新的DirectoryEntry(IMEM);
VAR AccountExpiration = DateTime.FromFileTime((int)的user.Properties [accountExpires]值。);
这是行不通的,只有给我的错误指定的转换无效。
当我使用
VAR AccountExpiration = user.Properties [accountExpires];
返回一个COM对象,我无法阅读。
使用Windows PowerShell,工作得很好,我不明白为什么这不会工作...
这是在code我在PowerShell中使用
$过期= [日期时间] :: FromFileTime($ tmpUser.accountExpires)
您可以使用 System.DirectoryServices.AccountManagement
命名空间来完成这个任务。一旦你获得了 UserPrincipal
从 PrincipalContext
,您可以检查 UserPrincipal.AccountExpirationDate
属性。
PrincipalContext上下文=新PrincipalContext(ContextType.Domain);
UserPrincipal P = UserPrincipal.FindByIdentity(背景下,域名\\用户名);
如果(p.AccountExpirationDate.HasValue)
{
日期时间过期= p.AccountExpirationDate.Value.ToLocalTime();
}
如果您做要使用的DirectoryEntry
,做到这一点:
//假设'用户'是的DirectoryEntry再presenting用户检查
日期时间期满= DateTime.FromFileTime(GetInt64(用户,accountExpires));
私人的Int64 GetInt64(的DirectoryEntry条目,串ATTR)
{
//我们将使用搜索的封送处理行为
DirectorySearcher从DS =新DirectorySearcher从(
条目,
的String.Format(({0} = *),attr)使用,
新的String [] {} ATTR,
SearchScope.Base
);
信息搜索结果SR = ds.FindOne();
如果(SR!= NULL)
{
如果(sr.Properties.Contains(attr)使用)
{
返回(Int64的)sr.Properties [ATTR] [0];
}
}
返回-1;
}
解析 accountExpires
价值的另一种方法是使用反射:
私有静态长ConvertLargeIntegerToLong(对象largeInteger)
{
类型类型= largeInteger.GetType();
INT highPart =(int)的type.InvokeMember(HighPart,BindingFlags.GetProperty,空,largeInteger,NULL);
INT lowPart =(int)的type.InvokeMember(LowPart,BindingFlags.GetProperty | BindingFlags.Public,空,largeInteger,NULL);
返回(长)highPart<< 32 | (UINT)lowPart;
}
对象accountExpires = DirectoryEntryHelper.GetAdObjectProperty(的DirectoryEntryaccountExpires);
VAR asLong = ConvertLargeIntegerToLong(accountExpires);
如果(asLong == long.MaxValue || asLong< = 0 || DateTime.MaxValue.ToFileTime()< = asLong)
{
返回DateTime.MaxValue;
}
其他
{
返回DateTime.FromFileTimeUtc(asLong);
}
I'm trying to retrieve the expiration date from accounts.
I've tried
DirectoryEntry user = new DirectoryEntry(iMem);
var AccountExpiration = DateTime.FromFileTime((int)user.Properties["accountExpires"].Value);
it doesn't work, only gives me the error "Specified cast is not valid".
When I use
var AccountExpiration = user.Properties["accountExpires"];
returns a com object, which I'm unable to read.
Using windows powershell, works fine, I don't get why this wont work...
this is the code I use in powershell
$Expires = [datetime]::FromFileTime($tmpUser.accountExpires)
You can use the System.DirectoryServices.AccountManagement
namespace to accomplish this task. Once you get a UserPrincipal
from a PrincipalContext
, you can inspect the UserPrincipal.AccountExpirationDate
property.
PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal p = UserPrincipal.FindByIdentity(context, "Domain\\User Name");
if (p.AccountExpirationDate.HasValue)
{
DateTime expiration = p.AccountExpirationDate.Value.ToLocalTime();
}
If you do want to use DirectoryEntry
, do this:
//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;
}
Another way of parsing the accountExpires
value is using reflection:
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);
}
这篇关于检索从ActiveDirectory的用户帐户过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!