2011-04-28 4 views
1

원래 ActiveDirectoryServices을 사용했지만 다른 멤버의 제안에 따라 ActiveDirectoryServices.AccountManagment 기반으로 전환했습니다. 함께 작업하는 것이 훨씬 쉬우나 한 가지 도전 과제를 제시합니다. LastPasswordSet을 반환하면 현지 시간 대신 UTC가 사용됩니다. 이 문제를 어떻게 해결할 수 있습니까?ActiveDirectoryServices.AccountManagment - LastPasswordSet - UTC time

감사합니다,

제이슨

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

날짜 시간은 정적 방법 SpecifyKind 있습니다. DateTime 객체가 현지 시간, UTC (Coordinated Universal Time)를 나타내거나 지역 시간 또는 UTC로 지정되지 않았는지 여부를 지정합니다. Here is the link to original Microsoft documentation.

인스턴스 메서드 ToLocalTime은 현재 DateTime 개체의 값을 현지 시간으로 변환합니다.

인스턴스 메서드 ToUniversalTime은 현재 DateTime 개체의 값을 UTC (Coordinated Universal Time)로 변환합니다.

3

변경

(DateTime)user.LastPasswordSet; 

user.LastPasswordSet.Value.ToLocalTime();