Introduction
The Windows Credentials dialog can be displayed using the CredUIPromptForCredentials
Win32 API call. This method of supplying the credentials dialog poses a few problems:
- Uses unmanaged code.
- Saved credentials are stored in the Windows Credentials store (not necessarily bad).
- Difficult implementation to use the search for user function on the dialog.
I needed a dialog that users were familiar with, but also provided the same functionality without the above restrictions. This dialog mimics the functionality of the Windows dialog with a few extra features.
- Fully contained in managed code.
- Developer can save credentials in their own store that is easily managed.
- Developer can easily implement a "search for user" function that will return a user name for the dialog.
Using the code
The dialog is implemented as a component and can be added directly to a form. When added, some properties and events are exposed that control the functionality of the dialog.
Properties and Events
ApplicationName |
String |
Gets or sets the name of the application. |
CustomBitmap |
Image |
Gets or sets the custom bitmap (320X57). |
Description |
String |
Gets or sets the description. |
EnableSearchForUser |
Boolean |
Indicates whether the search for user button is enabled. |
RememberCheckboxChecked |
Boolean |
Indicates whether the show remember checkbox is checked. |
ShowRememberCheckbox |
Boolean |
Indicates whether remember password checkbox is displayed. |
There are also various events that can be used to provide the extra features I needed.
QuerySearchForUser |
Occurs when user clicks the search for user button. Returns username selected in event arguments. |
QueryPreviouslyUsedUsernames |
Occurs when dialog displays so you can supply a list of previously used login names. Returns list of previously used usernames in event arguments. |
QueryInitialCredentials |
Occurs when dialog displays so you can supply previously used credentials. Returns username and password to be used in the event arguments. |
SaveCredentials |
Occurs when user clicks the OK button and the SavePassword checkbox is checked so the credentials can be saved. |
Example
After dropping a LoginDialog
on a form and setting the relevant properties, display the dialog like so:
private void Form1_Load(object sender, EventArgs e)
{
if (loginDialog1.ShowDialog(this) == DialogResult.OK)
{
string username = loginDialog1.Username;
string password = loginDialog1.Password;
}
}
private void loginDialog1_QueryInitialCredentials(object sender,
ref OSUtilties.EventArgs.QueryInitialCredentialsEventArgs e)
{
e.UserName = "username1";
if (e.SavePasswordEnabled) { e.Password = "mypassword"; }
}
private void loginDialog1_QueryPreviouslyUsedUsernames(object sender,
ref OSUtilties.EventArgs.QueryPreviouslyUsedUsernamesEventArgs e)
{
List<string> usernames = new List<string>();
usernames.Add("username1");
usernames.Add("maryjones");
usernames.Add("tester");
e.Usernames = usernames;
}
private void loginDialog1_QuerySearchForUser(object sender,
ref OSUtilties.EventArgs.QuerySearchForUserEventArgs e)
{
}
private void loginDialog1_SaveCredentials(object sender,
OSUtilties.EventArgs.SaveCredentialsEventArgs e)
{
}
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
With LoginDialog1
If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
Dim username as String = .Username
Dim password as String = .Password
End If
End With
End Sub
Private Sub LoginDialog1_QueryInitialCredentials(ByVal sender As System.Object, _
ByRef e As OSUtilties.EventArgs.QueryInitialCredentialsEventArgs) _
Handles LoginDialog1.QueryInitialCredentials
e.UserName = "username1"
If e.SavePasswordEnabled Then e.Password = "mypassword"
End Sub
Private Sub LoginDialog1_QueryPreviouslyUsedUsernames(ByVal sender As Object, _
ByRef e As OSUtilties.EventArgs.QueryPreviouslyUsedUsernamesEventArgs) _
Handles LoginDialog1.QueryPreviouslyUsedUsernames
Dim usernames As New List(Of String)
usernames.Add("username1")
usernames.Add("maryjones")
usernames.Add("tester")
e.Usernames = usernames
End Sub
Private Sub LoginDialog1_QuerySearchForUser(ByVal sender As Object, _
ByRef e As OSUtilties.EventArgs.QuerySearchForUserEventArgs) _
Handles LoginDialog1.QuerySearchForUser
End Sub
Private Sub LoginDialog1_SaveCredentials(ByVal sender As System.Object, _
ByVal e As OSUtilties.EventArgs.SaveCredentialsEventArgs) _
Handles LoginDialog1.SaveCredentials
End Sub
History
- 07/14/2011
Initial submission.