Click here to Skip to main content
15,881,455 members
Articles / Desktop Programming / Windows Forms

Windows Credentials Dialog Clone

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
14 Jul 2011CPOL2 min read 37.1K   1.6K   22   5
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

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.

Example

After dropping a LoginDialog on a form and setting the relevant properties, display the dialog like so:

C#
//
// 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
'
' 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)


Written By
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 Pin
BRTHELOVER20-Jul-14 21:35
BRTHELOVER20-Jul-14 21:35 
GeneralMy vote of 5 Pin
Blutfaust27-Jul-13 9:51
professionalBlutfaust27-Jul-13 9:51 
GeneralRe: My vote of 5 Pin
Greg Osborne29-Jul-13 3:29
Greg Osborne29-Jul-13 3:29 
BugHuge error!!! Pin
Igor Chistruga5-Oct-12 2:44
Igor Chistruga5-Oct-12 2:44 
QuestionFormatting Pin
Tom Deketelaere9-Aug-11 23:54
professionalTom Deketelaere9-Aug-11 23: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.