User Credentials CommandDialog with SecureString password
Now that VS2005 and .NET 2.0 is on the street, I started to
port some of my tools and projects from .NET 1.1. Since I
was working on authentication in a Winform client
application, one of the common scenarios is “credential
gathering”. This is typically a dialog that asks the user
credentials when logging on to the application or accessing
some restricted area of it.So I went back to get my UICredentialsHelper class in
NCrypto
project and not only ported to v2.0 but redesign it and
added dome goodies as well. You can download the code from
here. Let’s summarize its features:
-
Shows up the standard Windows dialog for credential
gathering (see
CredUIPromptForCredentials
API).
-
Inherits from CommonDialog so it has a base standard
API and IDE integration.
-
Return the sensitive information like the user password
in a
SecureString
type.
-
Flexible to configure according to the features exposed
by the CredUIPromptForCredentials API.
Sample Usage
Let’s see the basic usage of this dialog:
|
using (CredentialsDialog dialog = new CredentialsDialog()){ if
(dialog.ShowDialog() == DialogResult.OK) {
// validate credentials against an
authentication authority
// ...
// If credentials are valid
// and the user checked the "remember my
password" option
if
(dialog.SaveChecked) { dialog.ConfirmCredentials(true); } }}
|
The “dialog.ShowDialog()” function call will show up the
following dialog:
An interesting sample that you have included in the
downloaded solution is the one that can run a process under
the credentials account supplied.Let’s see the sample:
|
using (CredentialsDialog dialog = new CredentialsDialog()){ if
(dialog.ShowDialog() == DialogResult.OK) {
ProcessStartInfo info = new ProcessStartInfo("notepad.exe"); info.UseShellExecute = false; info.UserName = dialog.User; info.Password = dialog.Password; info.Domain = dialog.Domain;
using (Process install = Process.Start(info)) { install.WaitForExit();
Console.WriteLine(install.ExitCode); }
}
}
|
As you can see, is pretty straightforward to use this
dialog and you even can customize its appearance like
changing its caption, text message, banner bitmat or event
setting the user textbox as read only. Enjoy it!
This posting is provided "AS IS" with no warranties, and
confers no rights.