如果用户帐户处于活动状态,如何签入C#

can*_*zky 6 c# ldap

如何从C#检查本地用户帐户(即本地管理员帐户)是否处于活动状态?

我真正想要的是从"net user Administrator"命令输出"Account Active"="Yes"(或"No")的C#替换.

恐怕这个问题看起来像一个复制到一个,但我不知道在什么通过为根DirectoryEntry对象的参数.尝试过不同的东西,比如"ldap://"+ Environment.MachineName,"ldap://127.0.0.1","WinNT://"+ Environment.MachineName,但都没有.在所有三种情况下,我都会通过searcher.FindAll()调用抛出异常.

Bla*_*ICE 5

class Program
{
    static void Main(string[] args)
    {

        // Create the context for the principal object. 
        PrincipalContext ctx = new PrincipalContext(ContextType.Machine);

        UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "Administrator");
        Console.WriteLine(String.Format("Administrator is enable: {0}", u.Enabled));

    }
}
Run Code Online (Sandbox Code Playgroud)