Skip to content

Instantly share code, notes, and snippets.

@trepidity
Last active December 26, 2022 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepidity/5821244c6a8a2c4d97a6a0d94b5269dd to your computer and use it in GitHub Desktop.
Save trepidity/5821244c6a8a2c4d97a6a0d94b5269dd to your computer and use it in GitHub Desktop.
Create User in LDAP
using System;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Collections;
namespace BulkCreate
{
public class CreateUsers
{
private int ITERATIONS = 10000;
private int START_ITERATION = 5000;
public void start ()
{
CreateUser ("test");
}
private void CreateUser (string username)
{
LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier("10.10.10.10", 636);
LdapConnection con = new LdapConnection (ldi);
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallBack);
con.Credential = new NetworkCredential ("cn=admin,o=org", "password");
con.AuthType = AuthType.Basic;
try {
con.Bind();
Console.WriteLine("Bind success");
for (int i = START_ITERATION; i < ITERATIONS; i++) {
CreateUser(con, "loadtester" + i);
}
} catch (Exception e) {
Console.Write (e.Message);
}
}
private void CreateUser(LdapConnection con, string username)
{
AddRequest addme = new AddRequest(String.Format(@"cn={0}, ou=users,o=organization", username));
addme.Attributes.Add(new DirectoryAttribute("objectclass", new object[] { "inetorgPerson" }));
addme.Attributes.Add(new DirectoryAttribute("givenName", "new"));
addme.Attributes.Add(new DirectoryAttribute("sn", "user"));
addme.Attributes.Add(new DirectoryAttribute("description", "load test user - created August 5, 2016 JJennings"));
addme.Attributes.Add(new DirectoryAttribute("cn", "new user"));
addme.Attributes.Add(new DirectoryAttribute("userPassword", "password"));
try {
var response = con.SendRequest(addme);
Console.WriteLine("Successfully created " + username);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
private static bool ServerCallBack(LdapConnection connection, X509Certificate certificate)
{
return true;
}
}
}
@bitai-cs
Copy link

Hi @trepidity, Is this code functional?

@trepidity
Copy link
Author

As long as you update lines 22, 26, 45, with your environment details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment