Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.DirectoryServices;
- using System.DirectoryServices.ActiveDirectory;
- using System.DirectoryServices.AccountManagement;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
- DiableADUserUsingUserPrincipal(_ADUserName);
- }
- // Private Method
- private static void DiableADUserUsingUserPrincipal(string username)
- {
- try
- {
- PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
- UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
- (principalContext, username);
- userPrincipal.Enabled = false;
- userPrincipal.Save();
- MessageBox.Show("AD Account disabled for {0}", username);
- this.StatusTextBox.Text = "Account Disabled";
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- // Private Method
- private static void EnableADUserUsingUserPrincipal(string username)
- {
- try
- {
- PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
- UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
- (principalContext, username);
- userPrincipal.Enabled = true;
- userPrincipal.Save();
- MessageBox.Show("AD Account Enabled for {0}", username);
- this.StatusTextBox.Text = "Account Enabled";
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
- EnableADUserUsingUserPrincipal(_ADUserName);
- }
- }
- }
Add Comment
Please, Sign In to add comment