Click here to Skip to main content
13,297,411 members (65,890 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 14 Jul 2011

Windows Credentials Dialog Clone

, 14 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Clone of the Windows Credentials dialog.



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

ApplicationNameStringGets or sets the name of the application.
CustomBitmapImageGets or sets the custom bitmap (320X57).
DescriptionStringGets or sets the description.
EnableSearchForUserBooleanIndicates whether the search for user button is enabled.
RememberCheckboxCheckedBooleanIndicates whether the show remember checkbox is checked.
ShowRememberCheckboxBooleanIndicates whether remember password checkbox is displayed.

There are also various events that can be used to provide the extra features I needed.

QuerySearchForUserOccurs when user clicks the search for user button. Returns username selected in event arguments.
QueryPreviouslyUsedUsernamesOccurs when dialog displays so you can supply a list of previously used login names. Returns list of previously used usernames in event arguments.
QueryInitialCredentialsOccurs when dialog displays so you can supply previously used credentials. Returns username and password to be used in the event arguments.
SaveCredentialsOccurs when user clicks the OK button and the SavePassword checkbox is checked so the credentials can be saved.


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>();
    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 - 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)
    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


  • 07/14/2011
  • Initial submission.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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#

You may also be interested in...

Comments and Discussions

QuestionUsername -Empty value Pin
BRTHELOVER20-Jul-14 22:35
memberBRTHELOVER20-Jul-14 22:35 
GeneralMy vote of 5 Pin
Blutfaust27-Jul-13 10:51
memberBlutfaust27-Jul-13 10:51 
GeneralRe: My vote of 5 Pin
Greg Osborne29-Jul-13 4:29
memberGreg Osborne29-Jul-13 4:29 
BugHuge error!!! Pin
Igor Chistruga5-Oct-12 3:44
memberIgor Chistruga5-Oct-12 3:44 
QuestionFormatting Pin
Tom Deketelaere10-Aug-11 0:54
memberTom Deketelaere10-Aug-11 0:54 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 14 Jul 2011
Article Copyright 2011 by Greg Osborne
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid