How to check in C# if user account is active

2019-04-27 15:46发布

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.

标签: c# ldap
5条回答
贪生不怕死
2楼-- · 2019-04-27 16:34

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)

查看更多
3楼-- · 2019-04-27 16:35
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));

    }
}
查看更多
We Are One
4楼-- · 2019-04-27 16:37

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

查看更多
Root(大扎)
5楼-- · 2019-04-27 16:39

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;
查看更多
beautiful°
6楼-- · 2019-04-27 16:39

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

查看更多
登录 后发表回答