Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Active directory - new user

$
0
0

Hi guys I'm fairly noob at this and can do all the basic functions of a button when the method to apply to it is clear.

My question is, where in the below text would I put the button.click curly braces

private void buttonCreateusr_Click(object sender, EventArgs e)
     {

     }



to keep all code in its current context and essentially write the new user to active directory?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        string oGUID = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

      
     public string CreateUserAccount(string ldapPath, string userName, 
    string userPassword)
{
    try
    {
        userPassword = "metal123";
        ldapPath = "LDAP://OU=Aalco,OU=Aberdeen,DC=amari-metals,DC=.com"; //AD path - change aberdeen to variable on drop down
        userName = BoxFirst.Text; //Whatever is typed in to the user name box will define the username
        string connectionPrefix = "itsv.amari-metals.com" + ldapPath; // Connection to Active Directory
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix); //Appends prefix to directory object
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user"); //Adds an item to the above directory
        newUser.Properties["samAccountName"].Value = userName;
        newUser.CommitChanges(); //Applies the above code
        oGUID = newUser.Guid.ToString(); // Constructs parameters in to 

        newUser.Invoke("SetPassword", new object[] { userPassword }); // Sets password to that defined above
        newUser.CommitChanges(); // Applies the password
        dirEntry.Close(); //releases resources from AD
        newUser.Close(); //releases resources from AD
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        E.Message.ToString();

    }
    return oGUID;
            
}

     }
        }


    

Many thanks,

Elliot


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>