-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewPasswordForm.cs
110 lines (88 loc) · 3.36 KB
/
NewPasswordForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Cryptography;
using System.Windows.Forms;
namespace AdUserManager
{
public partial class NewPasswordForm : Form
{
public NewPasswordForm()
{
InitializeComponent();
}
UserPrincipal user;
public void LoadUser(UserPrincipal user)
{
this.user = user;
password1TextBox.UseSystemPasswordChar = true;
password1TextBox.Text = "";
password2TextBox.Text = "";
}
private void saveButton_Click(object sender, EventArgs e)
{
var password1 = password1TextBox.Text;
var password2 = password2TextBox.Text;
if (password1 != password2)
{
MessageBox.Show(this, "Passwords do not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (password1 == "")
{
var mbresult = MessageBox.Show(this, "Password is empty. User will not be able to login", "Empty password", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (mbresult.HasFlag(DialogResult.Cancel))
return;
}
SavePassword(password1);
}
private void generatePasswordButton_Click(object sender, EventArgs e)
{
int length = Properties.Settings.Default.generated_password_length;
bool includeSpecial = Properties.Settings.Default.generated_password_include_special;
string chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
if (includeSpecial)
chars += "+-*/.!?$_ ";
var rng = RandomNumberGenerator.Create();
byte[] rnd = new byte[length];
rng.GetBytes(rnd);
string password = "";
for (int i = 0; i < length; i++)
{
int m = rnd[i] % chars.Length;
password += chars[m];
}
password1TextBox.UseSystemPasswordChar = false;
password1TextBox.Text = password;
password2TextBox.Text = password;
}
private void SavePassword(string password)
{
try
{
bool? enabled = user.Enabled;
user.SetPassword(password);
user.UnlockAccount();
user.PasswordNotRequired = false;
user.Save();
// fix for disabled new user accounts
user.Enabled = enabled;
user.Save();
MessageBox.Show(this, "Password has been set", "Password saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
} catch (PasswordException ex)
{
MessageBox.Show(this, "Password does not meet security policy", "Weak password", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void NewPasswordForm_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
}