ActiveDirectoryServices.AccountManagment - LastPasswordSet - UTC 时间
我原本使用的是ActiveDirectoryServices
,但根据其他成员的建议切换到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;
}
}
I was originally using ActiveDirectoryServices
, but switch to ActiveDirectoryServices.AccountManagment
base on the suggestion of other members here. It's much easier to work with, but it is presenting one challenge. When returning the LastPasswordSet
, it is in UTC instead of the local time. How can I get around this?
Thanks,
Jason
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
Change the
to
DateTime 有一个静态方法
SpecifyKind
。它指定 DateTime 对象是否表示本地时间、协调世界时 (UTC),或者不指定为本地时间或 UTC。 这里是原始 Microsoft 文档的链接。实例方法
ToLocalTime
将当前DateTime对象的值转换为本地时间。实例方法
ToUniversalTime
将当前 DateTime 对象的值转换为协调世界时 (UTC)。DateTime has a static method
SpecifyKind
. It specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC), or is not specified as either local time or UTC. Here is the link to original Microsoft documentation.The Instance Method
ToLocalTime
converts the value of the current DateTime object to local time.The Instance Method
ToUniversalTime
converts the value of the current DateTime object to Coordinated Universal Time (UTC).