C # classes written for system users and group operations (users and groups can be added, deleted, and modified)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. directoryservices;

Namespace systemuseroperationdll
{
Public class systemuseroperationclass
{
Private directoryentry ad;

/// <Summary>
/// Initialization Method
/// </Summary>
/// <Param name = "strmachinename"> machine name </param>
/// <Param name = "strloginname"> logon username (for example, Administrator) </param>
/// <Param name = "strloginpassword"> logon password </param>
Public systemuseroperationclass (string strmachinename, string strloginname, string strloginpassword)
{
AD = new directoryentry ("winnt: //" + strmachinename + ", computer", ". \" + strloginname, strloginpassword );
}
/// <Summary>
/// Add a system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Param name = "strpassword"> password </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool adddomainuser (string strusername, string strpassword, string strdescription)
{
Try
{
Directoryentry newuser;
Newuser = AD. Children. Add (strusername, "user"); // Add a user
Newuser. Invoke ("setpassword", new object [] {strpassword}); // set the password
Newuser. properties ["Description"]. Add (strdescription); // Add description
Newuser. commitchanges ();

Ad. Close ();

}
Catch
{
Throw;
}
Return true;

}
/// <Summary>
/// Modify the system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Param name = "strpassword"> password </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool modifydomainuser (string strusername, string strpassword, string strdescription)
{
Try
{
Directoryentry user = AD. Children. Find (strusername );
If (user. Name! = NULL)
{
// Change the password
User. Invoke ("setpassword", new object [] {strpassword });
// Modify the description
User. properties ["Description"]. value = strdescription;
User. commitchanges ();
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Delete a system (domain) User
/// </Summary>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool deletedomainuser (string strusername)
{
Try
{
Directoryentry user = AD. Children. Find (strusername, "user"); // locate the user to delete
If (user. Name! = NULL)
{
Ad. Children. Remove (User );
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Add a system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool adddomaingroup (string strgroupname, string strdescription)
{
Try
{
Directoryentry group;
Group = AD. Children. Add (strgroupname, "group ");
Group. properties ["Description"]. Add (strdescription );
Group. commitchanges ();
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Modify the system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strdescription"> description </param>
/// <Returns> </returns>
Public bool modifydomaingroup (string strgroupname, string strdescription)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname );
If (group. Name! = NULL)
{
Group. properties ["Description"]. value = strdescription;
Group. commitchanges ();
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Delete the system (domain) Group
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Returns> </returns>
Public bool deletedomaingroup (string strgroupname)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group ");
If (group. Name! = NULL)
{
Ad. Children. Remove (group );
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Add group users
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool addgroupuser (string strgroupname, string strusername)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group"); // locate the group
Directoryentry user = AD. Children. Find (strusername, "user"); // find the user
If (group. Name! = NULL & User. Name! = NULL)
{
Group. Invoke ("add", new object [] {user. Path });
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}
/// <Summary>
/// Remove a group of users
/// </Summary>
/// <Param name = "strgroupname"> group name </param>
/// <Param name = "strusername"> User Name </param>
/// <Returns> </returns>
Public bool removegroupuser (string strgroupname, string strusername)
{
Try
{
Directoryentry group = AD. Children. Find (strgroupname, "group"); // locate the group

Object members = group. Invoke ("members", null );
Foreach (Object member in (system. Collections. ienumerable) members)
{
// Obtain each member of the group
Directoryentry x = new directoryentry (member );

If (strusername = x. Name) // if the user to be removed exists, the user is removed from the group.
{
Directoryentry user = AD. Children. Find (strusername, "user"); // locate the user
Group. Invoke ("Remove", new object [] {user. Path });
}
}
Ad. Close ();
}
Catch
{
Throw;
}
Return true;
}

}
}

 

Please kindly advise if you have any shortcomings in this regard for the first time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.