-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordResetDialog.vb
More file actions
88 lines (72 loc) · 3.41 KB
/
Copy pathPasswordResetDialog.vb
File metadata and controls
88 lines (72 loc) · 3.41 KB
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
Imports System.Windows.Forms
Imports System.Drawing
''' <summary>
''' Small modal dialog that asks an administrator for a new password, entered
''' twice and masked. Use the shared <see cref="Prompt"/> helper; it returns the
''' validated new password, or Nothing if the admin cancels.
''' Built entirely in code so it needs no Designer/.resx file.
''' </summary>
Public Class PasswordResetDialog
Inherits Form
Private Const MinPasswordLength As Integer = 4
Private ReadOnly txtNew As New TextBox()
Private ReadOnly txtConfirm As New TextBox()
Private ReadOnly lblError As New Label()
Public Property NewPassword As String = Nothing
Private Sub New(targetUser As String)
Me.Text = "Reset Password"
Me.FormBorderStyle = FormBorderStyle.FixedDialog
Me.StartPosition = FormStartPosition.CenterParent
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.ShowInTaskbar = False
Me.ClientSize = New Size(380, 226)
Dim lblInfo As New Label() With {
.Text = "Set a new password for: " & targetUser,
.Location = New Point(16, 14), .Size = New Size(348, 22),
.Font = New Font("Segoe UI", 9.0!, FontStyle.Bold)
}
Dim lblNew As New Label() With {.Text = "New password", .Location = New Point(16, 46), .Size = New Size(160, 20)}
txtNew.Location = New Point(16, 68) : txtNew.Size = New Size(348, 26)
txtNew.UseSystemPasswordChar = True
Dim lblConfirm As New Label() With {.Text = "Confirm password", .Location = New Point(16, 100), .Size = New Size(160, 20)}
txtConfirm.Location = New Point(16, 122) : txtConfirm.Size = New Size(348, 26)
txtConfirm.UseSystemPasswordChar = True
lblError.Location = New Point(16, 154) : lblError.Size = New Size(348, 20)
lblError.ForeColor = Color.Firebrick
Dim btnOk As New Button() With {.Text = "Reset", .Location = New Point(196, 184), .Size = New Size(80, 30)}
Dim btnCancel As New Button() With {.Text = "Cancel", .Location = New Point(284, 184), .Size = New Size(80, 30), .DialogResult = DialogResult.Cancel}
AddHandler btnOk.Click, AddressOf OnOk
Me.Controls.AddRange(New Control() {lblInfo, lblNew, txtNew, lblConfirm, txtConfirm, lblError, btnOk, btnCancel})
Me.AcceptButton = btnOk
Me.CancelButton = btnCancel
UiHelper.Polish(Me)
End Sub
Private Sub OnOk(sender As Object, e As EventArgs)
Dim pwd As String = txtNew.Text
If String.IsNullOrWhiteSpace(pwd) Then
lblError.Text = "Password is required."
Return
End If
If pwd.Length < MinPasswordLength Then
lblError.Text = $"Password must be at least {MinPasswordLength} characters."
Return
End If
If pwd <> txtConfirm.Text Then
lblError.Text = "Passwords do not match."
Return
End If
NewPassword = pwd
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
''' <summary>Show the dialog; returns the new password, or Nothing if cancelled.</summary>
Public Shared Function Prompt(owner As IWin32Window, targetUser As String) As String
Using dlg As New PasswordResetDialog(targetUser)
If dlg.ShowDialog(owner) = DialogResult.OK Then
Return dlg.NewPassword
End If
Return Nothing
End Using
End Function
End Class