在C#中为Windows Vista/7显示身份验证对话框

12

我想从用户那里获取网络登录凭据。

我使用的是C#和.NET 3.5。 迄今为止,我使用了 CredUIPromptForCredentials 调用(如何使用它的非常有用的链接可以在这里找到)。

问题在于,CredUIPromptForCredentials API调用显示旧的Windows 2000/XP凭据对话框,而不是新的Vista/7凭据对话框。

我在msdn上看到,应该使用CredUIPromptForWindowsCredentials 函数。

有人能发布一个使用C#的示例吗? 我还需要能够获取输入的凭据。

2个回答

20

我成功地实现了一个对我有用的解决方案。

以下是源代码:

    [DllImport("ole32.dll")]
    public static extern void CoTaskMemFree(IntPtr ptr);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct CREDUI_INFO
    {
        public int cbSize;
        public IntPtr hwndParent;
        public string pszMessageText;
        public string pszCaptionText;
        public IntPtr hbmBanner;
    }  


    [DllImport("credui.dll", CharSet = CharSet.Auto)]
    private static extern bool CredUnPackAuthenticationBuffer(int dwFlags,
                                                               IntPtr pAuthBuffer,
                                                               uint cbAuthBuffer,
                                                               StringBuilder pszUserName,
                                                               ref int pcchMaxUserName,
                                                               StringBuilder pszDomainName,
                                                               ref int pcchMaxDomainame,
                                                               StringBuilder pszPassword,
                                                               ref int pcchMaxPassword);

    [DllImport("credui.dll", CharSet = CharSet.Auto)]
    private static extern int CredUIPromptForWindowsCredentials(ref CREDUI_INFO notUsedHere,
                                                                 int authError,
                                                                 ref uint authPackage,
                                                                 IntPtr InAuthBuffer,
                                                                 uint InAuthBufferSize,
                                                                 out IntPtr refOutAuthBuffer,
                                                                 out uint refOutAuthBufferSize,
                                                                 ref bool fSave,
                                                                 int flags);



    public static void GetCredentialsVistaAndUp(string serverName, out NetworkCredential networkCredential)
    {
        CREDUI_INFO credui = new CREDUI_INFO();
        credui.pszCaptionText = "Please enter the credentails for " + serverName;
        credui.pszMessageText = "DisplayedMessage";
        credui.cbSize = Marshal.SizeOf(credui);
        uint authPackage = 0;
        IntPtr outCredBuffer = new IntPtr();
        uint outCredSize;
        bool save = false;
        int result = CredUIPromptForWindowsCredentials(ref credui,
                                                       0,
                                                       ref authPackage,
                                                       IntPtr.Zero,
                                                       0,
                                                       out outCredBuffer,
                                                       out outCredSize,
                                                       ref save,
                                                       1 /* Generic */);

        var usernameBuf = new StringBuilder(100);
        var passwordBuf  = new StringBuilder(100);
        var domainBuf = new StringBuilder(100);

        int maxUserName = 100;
        int maxDomain = 100;
        int maxPassword = 100;
        if (result == 0)
        {
            if (CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName,
                                               domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
            {
                //TODO: ms documentation says we should call this but i can't get it to work
                //SecureZeroMem(outCredBuffer, outCredSize);

                //clear the memory allocated by CredUIPromptForWindowsCredentials 
                CoTaskMemFree(outCredBuffer);
                networkCredential = new NetworkCredential()
                                        {
                                            UserName = usernameBuf.ToString(),
                                            Password = passwordBuf.ToString(),
                                            Domain = domainBuf.ToString()
                                        };
                return;
            }
        }

        networkCredential = null;
    }

我仍需要解决一些细节问题,比如如何记住上次输入的凭据等。

但主要部分已经可以了。


我看到你把函数命名为 GetCredentialsVistaAndUp,这个函数在XP上也能用吗?你测试过了吗? - Chrisjan Lodewyks
1
它不适用于XP。 XP不支持此对话框。它只能在Vista及更高版本中使用,就像方法名称所暗示的那样。 - codekaizen
我一直得到一个神秘的返回代码0x1F / 十进制31。结果发现我必须为所有内容设置CharSet = CharSet.Unicode,然后它就很好用了。 - Ray Ackley
你最终搞清楚如何获取最后输入的凭据了吗? - Thracx

2

以下是一些代码,摘自 bytes.com 帖子,供参考:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct _CREDUI_INFO
{
  public int cbSize;
  public IntPtr hwndParent;
  public string pszMessageText;
  public string pszCaptionText;
  public IntPtr hbmBanner;
}
class Program
{
  [DllImport("credui.dll", CharSet=CharSet.Unicode)]
  internal static extern uint CredUIPromptForWindowsCredentials(ref
    _CREDUI_INFO notUsedHere,
    int authError,
    ref uint authPackage,
    IntPtr InAuthBuffer,
    uint InAuthBufferSize,
    out IntPtr refOutAuthBuffer,
    out uint refOutAuthBufferSize,
    ref bool fSave,
    int flags);

  const int CREDUIWIN_AUTHPACKAGE_ONLY = 0x10;

  static void Main()
  {
    _CREDUI_INFO credui = new _CREDUI_INFO();
    credui.cbSize = Marshal.SizeOf(credui);
    credui.pszCaptionText = "Testje";
    credui.pszMessageText = "Message";
    uint authPackage = 0;
    IntPtr outCredBuffer;
    uint outCredSize;
    bool save = false;

    uint ret = CredUIPromptForWindowsCredentials(ref credui,
      0,
      ref authPackage,
      IntPtr.Zero,
      0,
      out outCredBuffer,
      out outCredSize,
      ref save,
      CREDUIWIN_AUTHPACKAGE_ONLY);

    if(ret != 0)
    {
      // failed to load function...
      // ...
    }
    else
    {
      // extract credentials from the buffer returned, using more
      //   credui.dll API's .
      // ...
    }
  }
}

我看到了这篇帖子。问题是我需要提取对话框中输入的凭据。我认为这需要使用CredUnPackAuthenticationBuffer API调用。 - Rubinsh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接