Click here to Skip to main content
Click here to Skip to main content
Go to top

Windows Credentials Dialog Clone

, 14 Jul 2011
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Greg Osborne

United States United States
Visual Basic Developer since version 1.0
Java web developer
Currently developing in vb and c#

Comments and Discussions

 
QuestionUsername -Empty value PinmemberBRTHELOVER20-Jul-14 21:35 
GeneralMy vote of 5 PinmemberBlutfaust27-Jul-13 9:51 
GeneralRe: My vote of 5 PinmemberGreg Osborne29-Jul-13 3:29 
BugHuge error!!! PinmemberIgor Chistruga5-Oct-12 2:44 
I have an error - on button OK press - for username value should be taken combobox's Text value not Selected.
QuestionFormatting PinmemberTom Deketelaere9-Aug-11 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 14 Jul 2011
Article Copyright 2011 by Greg Osborne
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid