---
title: "Windows Credential Management with the .net Framework 2.0(翻译)"
author: "eteran"
date: "2007-06-05T00:04:00Z"
tags: []
slug: "windows-credential-management-with-the-net-framework-2-0"
draft: false
lastmod: "2007-06-05T00:04:00Z"
id: 771921
url: "https://eteran.iteye.com/blog/771921"
---

原文链接  
&lt;meta name="ProgId" content="Word.Document"&gt; &lt;meta name="Generator" content="Microsoft Word 11"&gt; &lt;meta name="Originator" content="Microsoft Word 11"&gt; &lt;link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C03%5Cclip\_filelist.xml"&gt; &lt;link rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C03%5Cclip\_editdata.mso"&gt; &lt;!--\[if !mso]&gt; &lt;style&gt; v\\:* {behavior:url(#default#VML);} o\\:* {behavior:url(#default#VML);} w\\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} &lt;/style&gt; &lt;!\[endif]--&gt;&lt;!--\[if gte mso 9]&gt;&lt;xml&gt; &lt;w:worddocument&gt; &lt;w:view&gt;Normal&lt;/w:view&gt; &lt;w:zoom&gt;0&lt;/w:zoom&gt; &lt;w:punctuationkerning/&gt; &lt;w:drawinggridverticalspacing&gt;7.8 磅&lt;/w:drawinggridverticalspacing&gt; &lt;w:displayhorizontaldrawinggridevery&gt;0&lt;/w:displayhorizontaldrawinggridevery&gt; &lt;w:displayverticaldrawinggridevery&gt;2&lt;/w:displayverticaldrawinggridevery&gt; &lt;w:validateagainstschemas/&gt; &lt;w:saveifxmlinvalid&gt;false&lt;/w:saveifxmlinvalid&gt; &lt;w:ignoremixedcontent&gt;false&lt;/w:ignoremixedcontent&gt; &lt;w:alwaysshowplaceholdertext&gt;false&lt;/w:alwaysshowplaceholdertext&gt; &lt;w:compatibility&gt; &lt;w:spaceforul/&gt; &lt;w:balancesinglebytedoublebytewidth/&gt; &lt;w:donotleavebackslashalone/&gt; &lt;w:ultrailspace/&gt; &lt;w:donotexpandshiftreturn/&gt; &lt;w:adjustlineheightintable/&gt; &lt;w:breakwrappedtables/&gt; &lt;w:snaptogridincell/&gt; &lt;w:wraptextwithpunct/&gt; &lt;w:useasianbreakrules/&gt; &lt;w:dontgrowautofit/&gt; &lt;w:usefelayout/&gt; &lt;/w:compatibility&gt; &lt;w:browserlevel&gt;MicrosoftInternetExplorer4&lt;/w:browserlevel&gt; &lt;/w:worddocument&gt; &lt;/xml&gt;&lt;!\[endif]--&gt;&lt;!--\[if gte mso 9]&gt;&lt;xml&gt; &lt;w:latentstyles deflockedstate="false" latentstylecount="156"&gt; &lt;/w:latentstyles&gt; &lt;/xml&gt;&lt;!\[endif]--&gt;&lt;style&gt; &lt;!-- /* Font Definitions \*/ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions \*/ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h4 {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} pre {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions \*/ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --&gt; &lt;/style&gt; &lt;!--\[if gte mso 10]&gt; &lt;style&gt; /* Style Definitions \*/ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} &lt;/style&gt; &lt;!\[endif]--&gt;

Windows Credential Management with the .net Framework 2.0

Kenny kerr

January 2006

Applies to:

Microsoft .NET Framework 2.0  
NET Framework Security  
Windows XP

概叙：这篇文章主要是介绍Credential Management API怎样处理界面和用户的凭证集合。同时也提供了一个能够做一些简单凭证管理的类库，可以被c#和vb.net调用。

下载相关的例程：[KerrCredentialsSample.exe](http://download.microsoft.com/download/0/a/a/0aaee26d-24ae-4f5a-b3d9-047a86242e01/kerrcredentialssample.exe)

内容

介绍

请求凭证

托管代码的请求凭证

一个工具：请求凭证生成器

SecureString 基础

凭证集合

在托管代码中使用凭证集合

工具：凭证集合管理器

结论

 

 

介绍

管理用户凭证是一件非常困难的事情。理想上你的windows域凭证应该有足够的权限来访问你可能需要的资源。但是有这么简单吗？你可能不可避免的要去处理不同的安全权限，包括 windows域，Microsoft Passport和应用程序指定的验证策略。好像那些还不够挑战，凭证可能来自不同的form，包括smart cards, certificates 和密码。

 

自从2001年windows xp发布，windows包含了credential management api去管理用户凭证。这些api 的设计是为了简化内部程序用户凭证管理，也为你自己的相关凭证管理提供了一致的安全的方法。她也可以被用来请求不需要被持久化的凭证，或者持久化你的应用程序的凭证，比如用数据保护api（Data Protection API）.

 

在这篇文章我会介绍windows credential management api， 包括界面处理，凭证集合管理。介绍这些api的一个挑战是依赖你的背景知识和你怎么去用她，这些是一个c-style 的api，适用于visual c++或者任何一款支持windows的编译器。因为流行.net 框架的原因，一个理想的解决方案还应该包括.net 框架的支持托管代码的集合。所以也提供了一个功.net 框架调用的类库。

 

最后要说明的是： 当我解释windows credential management用到的不同的函数，结构，也包括列在表格中的相关的选项，标志和属性。虽然这些选项我的解释很多都是依赖与sdk和msndn类库，但是我不止是简单的从文档中copy说明，我是尽力想你说明她的一些你可能不能完全掌握的细节。这些都是基于我写credential management经验的积累。

 

请求凭证

如果你所需要得是你得域凭证，那么你不需要请求凭证。因为在你登陆会话时已经被验证，你可以访问你可能能访问的所有资源。明显的这是一个不现实的例子。所以你需要请求用户的凭证。在过去，不用的应用程序创建不用的用户界面来请求用户的凭证，但是在windows xp上，应用程序可以依靠内建的，简单的，安全的用户界面给用户输入。

Windows提供了CredUIPromptForCredentials函数显示一个可以配置的窗体去接收用户的凭证信息。

```
DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW info,
```

```
                                         PCWSTR targetName,
```

```
                                         PCtxtHandle reserved,
```

```
                                         DWORD errorCode,
```

```
                                         PWSTR userName,
```

```
                                         ULONG userNameBufferSize,
```

```
                                         PWSTR password,
```

```
                                           ULONG passwordBufferSize,
```

```
                                         PBOOL saveChecked,
```

```
DWORD flags);
```

```
因为这篇文章不是关于用c或者非托管的c++来管理用户凭证，所以我不对这个函数做过多细节的描叙。
```

```

 
```

```
第一个参数只向一个CREDUI_INFO结构的指针，它允许你指定它的父窗体和默认的图标，标题，信息。
```

```

 
```

```
targetName参数用来读取和写入凭证时的唯一定义，也被用来作为标题和信息的一部分，如果CREDUI_INFO没有被重写的话，晚些在我们讨论persisting凭证的时候你会对她认识的更深刻。
```

```
Errorcode 参数允许窗体提供相应的错误代码，但是sdk没有提供相应的链接，所以这个参数几乎没什么用。
```

```
username 和 userNameBufferSize参数用来指定初始化时的用户名。相应的password和passwordBufferSize用来指定初始化的用户密码。这些使得用c和非托管的c++编程相当困难，你必须非常小心的处理这些字符串，像你在下一章看到的，托管代码可以非常简单的写出非常优秀的代码。
```

```

 
```

```
这个窗体可以显示一个check box提供给用户来选择是否保存凭证。saveChecked 参数就是用来初始化这个check box 的状态用的。这个check box也可以在用户关闭窗体的时候来判断用户的喜好。注意的是这个参数被忽略，当凭证不是generic凭证的时候。
```

 

最好，falgs参数允许你指定一个联合的标识。这里的问题是错误的混合不同的标识也会导致程序的错误。

 

相比探索这个函数的更多细节，我将会提供一些为简单任务的解决方案给你，用托管代码写的。在那之前，我们先来看一看一个用C++写的例子。这个例子调用**CredUIPromptForCredentials**

 

```
CREDUI_INFO info = { sizeof (CREDUI_INFO) };
```

```
info.hwndParent = GetSafeHwnd();
```

```
 
```

```
info.pszCaptionText = L"Title";
```

```
info.pszMessageText = L"Message";
```

```
 
```

```
CBitmap bitmap;
```

```
VERIFY(bitmap.LoadBitmap(IDB_PROMPT_BITMAP));
```

```
info.hbmBanner = bitmap;
```

```
 
```

```
CString target;
```

```
const DWORD errorCode = 0;
```

```
 
```

```
wchar_t userName[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
```

```
 
```

```
wcscpy_s(userName,
```

```
         _countof(userName),
```

```
         L"initial user name");
```

```
 
```

```
wchar_t password[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
```

```
 
```

```
BOOL saveChecked = false;
```

```
 
```

```
Const DWORD flags = CREDUI_FLAGS_DO_NOT_PERSIST | 
```

```
                    CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX ;
```

```
 
```

```
DWORD result = ::CredUIPromptForCredentials(&info,
```

```
                                            target,
```

```
                                            0, // reserved
```

```
                                            errorCode,
```

```
                                            userName,
```

```
                                            _countof(userName)
```

```
                                            password,
```

```
                                            _countof(password)
```

```
                                            &saveChecked,
```

```
                                            flags);
```

```
 
```

```
switch (result)
```

```
{
```

```
    case NO_ERROR:
```

```
    {
```

```
        // User chose OK...
```

```
        break;
```

```
    }
```

```
    case ERROR_CANCELLED:
```

```
    {
```

```
        // User chose Cancel...
```

```
        break;
```

```
    }
```

```
    default:
```

```
    {
```

```
        // Handle all other errors...
```

```
    }
```

```
}
```

```
 
```

```
::SecureZeroMemory(password,
```

```
                   sizeof (password));
```

这段代码的结果是显示一下的窗体：

&lt;!--\[if gte vml 1]&gt;&lt;v:shapetype id="\_x0000\_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"&gt; &lt;v:stroke joinstyle="miter"/&gt; &lt;v:formulas&gt; &lt;v:f eqn="if lineDrawn pixelLineWidth 0"/&gt; &lt;v:f eqn="sum @0 1 0"/&gt; &lt;v:f eqn="sum 0 0 @1"/&gt; &lt;v:f eqn="prod @2 1 2"/&gt; &lt;v:f eqn="prod @3 21600 pixelWidth"/&gt; &lt;v:f eqn="prod @3 21600 pixelHeight"/&gt; &lt;v:f eqn="sum @0 0 1"/&gt; &lt;v:f eqn="prod @6 1 2"/&gt; &lt;v:f eqn="prod @7 21600 pixelWidth"/&gt; &lt;v:f eqn="sum @8 21600 0"/&gt; &lt;v:f eqn="prod @7 21600 pixelHeight"/&gt; &lt;v:f eqn="sum @10 21600 0"/&gt; &lt;/v:formulas&gt; &lt;v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/&gt; &lt;o:lock v:ext="edit" aspectratio="t"/&gt; &lt;/v:shapetype&gt;&lt;v:shape id="\_x0000\_i1025" type="#\_x0000\_t75" style="'width:246pt; height:218.25pt'"&gt; &lt;v:imagedata src="file:///C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\msohtml1\\03\\clip\_image001.png" o:title=""/&gt; &lt;/v:shape&gt;&lt;!\[endif]--&gt;&lt;!--\[if !vml]--&gt;![](http://www.cnblogs.com/images/cnblogs_com/eteran/image1.GIF)&lt;!--\[endif]--&gt;

正像你所看到的，有一大段代码是为了调用这个函数不会出错，首先，你要熟悉结构CREDUI\_INFO去控制这个窗体的不同元素。接下来，你要为接收的用户名合密码准备缓冲区。和显示初始值。CREDUI\_MAX\_USERNAME\_LENGTH常量表示用户名的最大长度。相同的CREDUI\_MAX\_PASSWORD\_LENGTH表示密码的可能最大长度。在用户名和密码长度后面加1是为了加上介绍字符串NULL, 调用wcscpy\_s函数是为了显示用户名和密码的初始值。最后，我指定了两个标识，意思是，我不想persist 这个凭证但是我想让保存密码的check box 显示。saveChecked将会包括用户的选项。

显然在这里你很容易出错。

 

请求凭证，用托管代码

因为CredUIPromptForCredentials

#### \[C++]

```
ref class MainWindow : Windows::Forms::Form
```

```
{
```

```
public:
```

```

 
```

```
    void Prompt()
```

```
    {
```

```
        PromptForCredentials dialog;
```

```

 
```

```
        dialog.Title = "Title";
```

```
        dialog.Message = "Message";
```

```
        dialog.Banner = Images::Banner;
```

```
        dialog.UserName = "initial user name";
```

```

 
```

```
        dialog.DoNotPersist = true;
```

```
        dialog.ShowSaveCheckBox = true;
```

```

 
```

```
        if (Windows::Forms::DialogResult::OK == dialog.ShowDialog(this))
```

```
        {
```

```
            // Use credentials wisely...
```

```
        }
```

```
    }
```

```
};
```

#### \[C#]

```
class MainWindow : Form
```

```
{
```

```
    public void Prompt()
```

```
    {
```

```
        using (PromptForCredentials dialog = new PromptForCredentials())
```

```
        {
```

```
            dialog.Title = "Title";
```

```
            dialog.Message = "Message";
```

```
            dialog.Banner = Images.Banner;
```

```
            dialog.UserName = "initial user name";
```

```

 
```

```
            dialog.DoNotPersist = true;
```

```
            dialog.ShowSaveCheckBox = true;
```

```

 
```

```
            if (DialogResult.OK == dialog.ShowDialog(this))
```

```
            {
```

```
                // Use credentials wisely...
```

```
            }
```

```
        }
```

```
    }
```

```
}
```