2011-04-28 49 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();