Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows Credentials Dialog Clone

0.00/5 (No votes)
14 Jul 2011 1  
Clone of the Windows Credentials dialog.

dialog.png

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:

//
// C# - example
//
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)
{
    //if you want to cancel set e.Cancel = true;
    e.UserName = "username1";
    if (e.SavePasswordEnabled) { e.Password = "mypassword"; }
}

private void loginDialog1_QueryPreviouslyUsedUsernames(object sender, 
        ref OSUtilties.EventArgs.QueryPreviouslyUsedUsernamesEventArgs e)
{
    //you can take this from any source
    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)
{
    //here is where you would supply your own dialog to search for a user
}

private void loginDialog1_SaveCredentials(object sender, 
        OSUtilties.EventArgs.SaveCredentialsEventArgs e)
{
    //here is where you would save credentials for later use
}
VB.NET:
'
' VB.NET - example
'
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
    'you can take this from any source
    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
    'here is where you would supply your own dialog to search for a user
End Sub

Private Sub LoginDialog1_SaveCredentials(ByVal sender As System.Object, _
        ByVal e As OSUtilties.EventArgs.SaveCredentialsEventArgs) _
        Handles LoginDialog1.SaveCredentials
    'here is where you would save credentials for later use
End Sub

History

  • 07/14/2011
  • Initial submission.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here