Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in C# if user account is active

Tags:

c#

ldap

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.

like image 423
candritzky Avatar asked Sep 07 '12 13:09

candritzky


People also ask

How do you check if it is a number C?

The isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it's a digit else it returns 0. For example, it returns a non-zero value for '0' to '9' and zero for others. The isdigit() is declared inside ctype.

What is scanf in C with example?

In C programming language, scanf is a function that stands for Scan Formatted String. It reads data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer.

How do you scan a sentence in C?

The syntax for using scanf()function in C : scanf("%s", char *s); Here, s is the pointer which points to the array of characters where the input taken as a string will be stored. Note that in the syntax, we don't use the & symbol with s.


2 Answers

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

    }
}
like image 117
BlackICE Avatar answered Oct 13 '22 07:10

BlackICE


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)

like image 25
Alex K. Avatar answered Oct 13 '22 06:10

Alex K.