Friday, September 20, 2024

Using the Network Functions in C# (Part I – User Functions)

Welcome to another “How-to” article in Visual C#.NET. This article will focus on the Network functions within the Win32 API library associated with Network Management. The first thing is to point out is that there are two ways to manage users using the .NET Framework, the first being the Active Directory method, as you will notice you need to have Active Directory Installed and if you were managing users on a small network or on a stand alone station Active Directory won’t be install and it would be not worth installing it.

Therefore the other method and focus of this article is using the Platform Invoke layer and the Network functions of the Win32 API library. In this article we will look at how to Add, Delete and Modify Users and Groups using C# and also how we can query User and Network information for a machine or network, we will look at the following functions:

  • NetUserAdd
  • NetUserDel
  • NetUserGetInfo
  • NetUserSetInfo
  • NetUserChangePassword
  • NetUserEnum
  • NetUserGetLocalGroups

Getting Started

First of all as I’m sure most C# developers out there know we have to include the InteropServices namespace in our project to allow us to access functions from DLL’s and this can be done using the following snippet of code:

-==-

Once we have included the access we can now include the declarations of the DLL’s we are going to use and the Structures (structs) associated with them. Each of the calls needed will be discussed under the headings relating to what they do.

Adding a User using C#

One of the most important operations within the Network functions is that of Adding Users to either a network or Computer. To Add a user via C# we will need to use the NetAddUser function, using this function allows us to specify a machine to add the user to or we can leave that null an the User is added to the local machine. In the below code snippet we can see how we can declare and use the NetUserAdd function. One important item associated with the NetUserAdd function is that the user you are added must be in the form of a specific structure. The structures (structs) that are used in association with this call are USER INFO 1, USER INFO 2, USER INFO 3 or USER INFO 4. In this practical example we shall use USER_INFO_1 to define our UserArea to add.

-==-

You should also note that when i used this code, i didn’t need to know about the errors returned, hence the use of an int as the last parameter, if you did want to know about errors returned you would need to amend this code. Now that we have declared our external API to use we should also include our USER_INFO_1 structure, using the following code snippet:

-==-

Once we have both of these block of code declare we can now use the call from within our program, i should also note that in this case we have used the structure about you can use any of the other three, and if you click the link you can convert the code from C++ to C# pretty easily. Below is a code snippet to show the usage of the NetUserAdd function.

-==-

So as we discussed the above code will add a user to the machine you are currently on, but as I said if you want to add the user to a different machine on you network you can replace the first null parameter of the call with the name of the machine.

Removing a User using C#

Compared to the previous function removing a user is far easier, as with the above code the function returns a non-zero value if it fails, to remove a user from the local machine you can use the following declaration in the below code snippet.

-==-

The usage for the NetUserDel called would be as shown below in the code snippet.

-==-

As also with the NetUserAdd call, the user can make this call to remote machines by replacing the null string in the first parameter with that of the unicode representation or the computers name.

Getting User Information then modifying it using C#

To obtain user information within Visual C#.NET we need to used the native call NetUserGetInfo, this call as with NetUserAdd uses a structure (struct) to manage the data, however in this case it returns data to a struct as opposed to taking it out. In association with NetUserGetInfo, to change the information you have obtained you can use the function NetUserSetInfo. You should also note that these functions rely on each other to allow changes, for example to use the NetUserSetInfo function you must know the users privilege level, which is obtained via the NetUserGetInfo function. In the below code snippet we have the required declarations for both calls and please NOTE for this function we are using the USER_INFO_1 structure from above again.

-==-

Using these declarations we can obtain and modify a users settings, with ease, if we look at the next code snippet we will obtain the user information for the user we made earlier “UserTestOne” and then we will change some user information.

-==-

-==-

To summarize, NetUserSetInfo relies on NetUserGetInfo for it to work correctly, and as with all the other Network functions, if you change the null string in the first parameter to that of a computer, then you can use the function remotely.

Changing a Users Password using C#

Another very important function of a network management program is to allow the changing or passwords. The function NetUserChangePassword, allows us to do this bascially providing we have the user specified current password. So this function could be considered to be used as part of a program that allows a user to change their own password. Anyway to declare the NetUserChangePassword function we use the following code snippet.

-==-

Using this declaration we can now manipulate a set users password, providing we know it already. Again we can use the below code remotely changing the null string parameter to that of the machine to manage.

-==-

Obtaining the list of Users using C#

When managing a network it is also important what we have a accurate list of the users on the network, or local machine. To obtain this we must use the NetUserEnum function, which returns the names and data elements of each user area to structures. In this case we are going to use the USER_INFO_0 structure to pass the return values into, simply because we are only trying to get usernames no other data i have added this in the declarations section. It is also important to note that this function uses the network buffer, which must be freed after, to free resources, this is done using the NetAPIBufferFree function also listed. To start with in this code snippet is the declarations.

-==-

Once we have made the declaration we can go out and use our code, to create the collection of the users, to do this we use the below code snippet.

-==-

The above code intern returns each use listed on the local machine via MessageBox, as with all other function you can chose a remote computer again by replacing the null string.

Identifying a users group membership using C#

The one last function we need to look at in this article is NetUserGetLocalGroups. This function allows us to determine what Groups the user is a member of and display them. As with the previous method we have to flush the NetAPIBuffer after using the function, using the same declaration as earlier. The declaration for NetUserGetLocalGroups also needs the LOCALGROUP_USERS_INFO_0 structure to return the names to this along with the declaration are shown in the below code snippet.

-==-

With the above declaration we can now call it using similar code as in the previous functions, looking something like the below code snippet.

-==-

The above example returns the groups to which the user Administrator belongs, again you can specify any user and any computer name. In this article we has looked at and discussed the User related Network Function available through Platform Invoke on the .NET Framework. So you can see that all this code does in fact work, i have included a sample Example application with all the function working in it. Available for download below.

Network Function Examples ~
Visual Studio .NET Project + Source (49K)
Runtime EXE (28K)

First published by CSharpHelp

My name is Michael Bright, I’m a university student studying a Bsc in Computer Science at University College Chester. I have a love for all thing computers and have only worked with C# for about 3 months. I am currently training for my MCP in it. My other interests are C, VB, HTML, ASP and also Flash. I have interests in Developing Network app’s and Network Security. My Web site is csharp.brightweb.co.uk where you can find examples of my Work, including my Defender Security applications.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles