简体   繁体   中英

How to check in C# if user account is active

How can I check from C# if a local user account (namely the local Administrator account) is active?

What I actually want is a C# replacement for the "Account Active" = "Yes" (or "No") output from the "net user Administrator" command.

I'm afraid this question looks like a duplicate to this one, but I don't know what to pass in for the parameter for the root DirectoryEntry object. Tried different things like "ldap://" + Environment.MachineName, "ldap://127.0.0.1", "WinNT://" + Environment.MachineName, but none of them worked. I get an exception thrown by the searcher.FindAll() call in all three cases.

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));

    }
}

You can query WMI's Win32_UserAccount

This is boilerplate what MS's wmi code creator spits out as a reference;

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Disabled FROM Win32_UserAccount WHERE name = 'alexk'");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_UserAccount instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Disabled: {0}", queryObj["Disabled"]);
                    Console.ReadKey();
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

(I'd link the tool but as usual the msdn links are dead)

Try this.

 var server = "YOURMACHINENAME";
 var username = "Guest"; 
 var de = new DirectoryEntry {Path = "WinNT://" + server + ",computer"};
 var result = de.Children
     .Cast<DirectoryEntry>()
     .First<DirectoryEntry>(d => d.SchemaClassName == "User" && d.Properties["Name"].Value.ToString() == username);

 var flags = (int)result.Properties["UserFlags"].Value;
 var disabled = (flags & 2) == 2;

This isn't quite the same but they use DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username)); Would that help?

Considering it's a local user, you need to call the win32 api funcion NetGetUserInfo to get what you need.

The example in pinvoke.net is almost what you need, however you need to change the level parameter to 2 to get the neccesary info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM