2011-04-28 68 views
1

我最初使用的是ActiveDirectoryServices,但根据其他成员的建议在此处切换到ActiveDirectoryServices.AccountManagment。与合作更容易,但它提出了一个挑战。当返回LastPasswordSet时,它是UTC而不是当地时间。我怎样才能解决这个问题?ActiveDirectoryServices.AccountManagment - LastPasswordSet - UTC时间

感谢,

杰森

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; 
    } 
} 

回答

0

的DateTime有一个静态方法SpecifyKind。它指定DateTime对象是表示本地时间,协调世界时(UTC)还是未指定为本地时间或UTC。 Here is the link to original Microsoft documentation

实例方法ToLocalTime将当前DateTime对象的值转换为本地时间。

实例方法ToUniversalTime将当前DateTime对象的值转换为协调世界时(UTC)。

3

更改

(DateTime)user.LastPasswordSet; 

user.LastPasswordSet.Value.ToLocalTime();