aacable79

Untitled

Mar 29th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.DirectoryServices;
  11. using System.DirectoryServices.ActiveDirectory;
  12. using System.DirectoryServices.AccountManagement;
  13. namespace WindowsFormsApplication2
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22.  
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
  26. DiableADUserUsingUserPrincipal(_ADUserName);
  27. }
  28.  
  29. // Private Method
  30. private static void DiableADUserUsingUserPrincipal(string username)
  31. {
  32. try
  33. {
  34. PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
  35. UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
  36. (principalContext, username);
  37. userPrincipal.Enabled = false;
  38. userPrincipal.Save();
  39. MessageBox.Show("AD Account disabled for {0}", username);
  40. this.StatusTextBox.Text = "Account Disabled";
  41. }
  42. catch (Exception ex)
  43. {
  44. Console.WriteLine(ex.Message);
  45. }
  46. }
  47.  
  48. // Private Method
  49. private static void EnableADUserUsingUserPrincipal(string username)
  50. {
  51. try
  52. {
  53. PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
  54. UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
  55. (principalContext, username);
  56. userPrincipal.Enabled = true;
  57. userPrincipal.Save();
  58. MessageBox.Show("AD Account Enabled for {0}", username);
  59. this.StatusTextBox.Text = "Account Enabled";
  60.  
  61. }
  62. catch (Exception ex)
  63. {
  64. Console.WriteLine(ex.Message);
  65. }
  66. }
  67.  
  68.  
  69. private void button2_Click(object sender, EventArgs e)
  70. {
  71. String _ADUserName = textBox1.Text; // <-- The textbox you enter your username?
  72. EnableADUserUsingUserPrincipal(_ADUserName);
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment