C# 使用C查找windows 10上的当前用户登录时间#

C# 使用C查找windows 10上的当前用户登录时间#,c#,windows,authentication,cmd,C#,Windows,Authentication,Cmd,我需要获取当前用户登录时间,与当前用户会话启动时相同,现在我使用以下cmd代码获取: for /f "skip=1 tokens=5*" %a in ('quser %username%') do @echo USER SESSION STARTED AT: %b 我想找到一种使用C#获取密码的方法,但到目前为止,我只找到了获取用户上次登录时插入密码的方法(如果上次用户使用密码登录,则两个时间戳相等,但如果用户自动登录或在Windows 10上使用PIN,则两个时间戳不相等) 我如何做(除了解

我需要获取当前用户登录时间,与当前用户会话启动时相同,现在我使用以下cmd代码获取:

for /f "skip=1 tokens=5*" %a in ('quser %username%') do @echo USER SESSION STARTED AT: %b
我想找到一种使用C#获取密码的方法,但到目前为止,我只找到了获取用户上次登录时插入密码的方法(如果上次用户使用密码登录,则两个时间戳相等,但如果用户自动登录或在Windows 10上使用PIN,则两个时间戳不相等)

我如何做(除了解析quser.exe输出)


提前感谢

可能您可以尝试以下方式:

using System.DirectoryServices.AccountManagement;

public static DateTime? GetLastLoginToMachine(string machineName, string userName)
{
    PrincipalContext c = new PrincipalContext(ContextType.Machine, machineName);
    UserPrincipal uc = UserPrincipal.FindByIdentity(c, userName);
    return uc.LastLogon;
}

这回答了你的问题吗?谢谢你的回答。我已经尝试过这个代码,它相当于cmd代码“net user%username%”的“last access”字段,它给出了当前用户最后一次插入密码的时间,并且忽略了自动登录或使用pin登录。