我最初使用的是ActiveDirectoryServices,但根据其他成员的建议改用ActiveDirectoryServices.AccountManagment。使用它要容易得多,但它带来了一个挑战。当返回LastPasswordSet时,它使用协调世界时而不是本地时间。我怎么才能避免这个问题呢?
谢谢,
杰森
public UserPrincipal GetUser(string sUserName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal =
UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
if (oUserPrincipal != null)
{
BuildUser(oUserPrincipal);
}
return oUserPrincipal;
}
private void BuildUser(UserPrincipal user)
{
//Populate the user with items available in the UserPrincipal object
if (user != null)
{
if (user.LastPasswordSet.HasValue)
this.PasswordLastSet = (DateTime)user.LastPasswordSet;
}
}发布于 2011-04-29 02:52:33
DateTime有一个静态方法SpecifyKind。它指定DateTime对象是表示本地时间、协调世界时,还是未指定为本地时间或协调世界时。Here is the link to original Microsoft documentation。
实例方法ToLocalTime将当前DateTime对象的值转换为本地时间。
实例方法ToUniversalTime将当前DateTime对象的值转换为协调世界时。
发布于 2011-04-29 04:59:14
更改
(DateTime)user.LastPasswordSet;至
user.LastPasswordSet.Value.ToLocalTime();https://stackoverflow.com/questions/5821927
复制相似问题