Windows凭据对话框克隆

介绍 Windows证书对话框可以显示使用CredUIPromptForCredentials Win32 API调用。该方法提供的凭证对话提出了几个问题: 使用非托管代码。保存凭证存储在Windows凭证存储(不一定是坏的)。困难的实现为用户使用搜索功能的对话框。 我需要一个对话框,用户很熟悉,但也提供了相同的功能没有上述限制。这个对话框模仿Windows对话框的功能有一些额外的特性。 完全包含在托管代码。开发人员可以凭证保存在自己的商店,很容易管理。开发人员可以很容易地实现一个“搜索用户”函数将返回一个用户名称对话框。 使用的代码 对话框中被实现为一个组件,可以直接添加一个表单。当补充道,一些属性和事件暴露控制对话框的功能。 属性和事件 ApplicationNameStringGets或设置应用程序的名称。CustomBitmapImageGets或设置自定义位图(320 x57)。DescriptionStringGets或设置描述。EnableSearchForUserBooleanIndicates搜索用户按钮是否启用。RememberCheckboxCheckedBooleanIndicates显示记得复选框是否选中。ShowRememberCheckboxBooleanIndicates是否记住密码复选框显示。 还有各种事件,可用于提供我需要的额外特性。 QuerySearchForUserOccurs当用户单击搜索用户按钮。返回用户名中选择事件参数。QueryPreviouslyUsedUsernamesOccurs当对话框显示你可以供应之前使用登录名称的列表。返回之前用户名用于事件的参数列表。QueryInitialCredentialsOccurs当对话框显示之前你可以供应使用凭证。返回用户名和密码用于事件参数。SaveCredentialsOccurs当用户单击OK按钮,SavePassword复选框选中的凭证可以保存。 例子 下降后LoginDialog形式和设置相关属性,显示对话框如下: 隐藏,收缩,复制Code

//
// 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: 隐藏,收缩,复制Code

'
' 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

历史 07/14/2011Initial提交。 本文转载于:http://www.diyabc.com/frontweb/news5381.html

posted @ 2020-08-09 14:57  Dincat  阅读(244)  评论(0编辑  收藏  举报