Beispiel #1
0
        public static List <string> EnumerateAllUsers()
        {
            List <string> result = new List <string>();
            int           entriesRead;
            int           totalEntries;
            int           resume;

            IntPtr    bufPtr;
            const int Level = 0;
            const int FILTER_NORMAL_ACCOUNT = 2;

            NetUserEnum(null, Level, FILTER_NORMAL_ACCOUNT, out bufPtr, -1, out entriesRead, out totalEntries, out resume);

            if (entriesRead > 0)
            {
                USER_INFO_0[] Users = new USER_INFO_0[entriesRead];
                IntPtr        iter  = bufPtr;
                for (int i = 0; i < entriesRead; i++)
                {
                    Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                    iter     = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                    result.Add(Users[i].Username);
                }
                NetApiBufferFree(bufPtr);
            }
            return(result);
        }
Beispiel #2
0
        //Get all of system's user's name of current pc.
        public List <string> GetSysUserNames()
        {
            int           EntriesRead;
            int           TotalEntries;
            int           Resume;
            IntPtr        bufPtr;
            List <string> temp = new List <string>();

            NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead,
                        out TotalEntries, out Resume);
            if (EntriesRead > 0)
            {
                USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                IntPtr        iter  = bufPtr;
                for (int i = 0; i < EntriesRead; i++)
                {
                    Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
                                                                   typeof(USER_INFO_0));
                    iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                    temp.Add(Users[i].Username);
                }
                NetApiBufferFree(bufPtr);
            }
            return(temp);
        }
Beispiel #3
0
        public static List <string> GetAllUsersOfSystem(string ip)
        {
            List <string> users = new List <string>();

            int    EntriesRead;
            int    TotalEntries;
            int    Resume;
            IntPtr bufPtr;

            NetUserEnum(ip, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);

            if (EntriesRead > 0)
            {
                USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                IntPtr        iter  = bufPtr;

                for (int i = 0; i < EntriesRead; i++)
                {
                    Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                    iter     = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                    users.Add(Users[i].UserName);
                }
                NetApiBufferFree(bufPtr);
            }
            return(users);
        }
Beispiel #4
0
        public static List <string> Get_LocalUser(Args_Get_DomainUser args = null)
        {
            List <string> users = new List <string>();
            int           EntriesRead;
            int           TotalEntries;
            int           Resume;
            IntPtr        bufPtr;

            NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);

            if (EntriesRead > 0)
            {
                Logger.Write_Output("[Get-LocalUser] Found " + EntriesRead + " user.");
                USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                IntPtr        iter  = bufPtr;
                for (int i = 0; i < EntriesRead; i++)
                {
                    Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                    iter     = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                    users.Add(Users[i].UserName);
                }
                NetApiBufferFree(bufPtr);
            }
            else
            {
                Logger.Write_Warning("[Get-LocalUser] Error, Cann't found any user.");
            }
            return(users);
        }
Beispiel #5
0
        public static List <string> EnumerateAllUsers()
        {
            List <string> result = new List <string>();
            uint          entriesRead;
            uint          totalEntries;
            uint          resume;

            IntPtr bufPtr;
            uint   level  = 0;
            uint   status = NetUserEnum(null, level, FILTER_NORMAL_ACCOUNT, out bufPtr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, out resume);

            if (status != NERR_Success)
            {
                throw new Exception("NetUserEnum failed, Error code: " + status.ToString());
            }

            if (entriesRead > 0)
            {
                USER_INFO_0[] Users = new USER_INFO_0[entriesRead];
                IntPtr        iter  = bufPtr;
                for (int i = 0; i < entriesRead; i++)
                {
                    Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                    iter     = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_0)));
                    result.Add(Users[i].Username);
                }
                NetApiBufferFree(bufPtr);
            }
            return(result);
        }
Beispiel #6
0
        public static Collection<string> EnumerateUsers()
        {
            int entriesRead;
            int totalEntries;
            int resume;
            IntPtr bufPtr;
            var userName = new Collection<string>();

            NetUserEnum(null, 0, 2, out bufPtr, -1, out entriesRead, out totalEntries, out resume);

            if (entriesRead > 0)
            {
                var users = new USER_INFO_0[entriesRead];
                IntPtr iter = bufPtr;
                for (int i = 0; i < entriesRead; i++)
                {
                    users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                    iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                    userName.Add(users[i].Username);
                }
                NetApiBufferFree(bufPtr);
            }
            return userName;
        }
Beispiel #7
0
        NetRenameUser(
            string servername,
            string oldusername,
            string username
            )
        {
            uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS;

            if (String.IsNullOrEmpty(servername))
            {
                 servername = null;
            }
            if (String.IsNullOrEmpty(oldusername))
            {
                 oldusername = null;
            }
            if (String.IsNullOrEmpty(username))
            {
                 username = null;
            }

            USER_INFO_0 usrinfo_0 = new USER_INFO_0();
            usrinfo_0.usri0_name = username;

            IntPtr bufptr = IntPtr.Zero;
            bufptr = Marshal.AllocHGlobal(Marshal.SizeOf(usrinfo_0));

            try
            {
                if (!NetApiInitCalled)
                {
                    result = NetApiInit();
                    if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS)
                    {
                        return result;
                    }

                    NetApiInitCalled = true;
                }

                Marshal.StructureToPtr(usrinfo_0, bufptr, false);

                result = (uint)NetUserSetInfo(servername, oldusername, 0, bufptr, IntPtr.Zero);
            }
            catch (Exception)
            {
                result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE;
            }
            finally
            {
                Marshal.DestroyStructure(bufptr, usrinfo_0.GetType());
                Marshal.FreeHGlobal(bufptr);
            }

            return result;

        }