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

在C#for windows Vista/7中显示身份验证对话框,c#,pinvoke,C#,Pinvoke,我想从用户处获取网络登录凭据 我将.NET3.5与C#一起使用。 到目前为止,我使用了CredUIPromptForCredentials调用 (可以找到关于如何使用它的非常有用的链接) 我的问题是,CredUIPromptForCredentialsAPI调用显示的是旧的windows2000/XP凭据对话框,而不是新的Vista/7对话框 我在msdn上读到我应该使用这个函数 有人能举个例子说明如何将它与C#一起使用吗? 我还需要能够获取输入的凭据。以下是一些代码: 我设法实现了一个适合我的

我想从用户处获取网络登录凭据

我将.NET3.5与C#一起使用。 到目前为止,我使用了
CredUIPromptForCredentials
调用 (可以找到关于如何使用它的非常有用的链接)

我的问题是,
CredUIPromptForCredentials
API调用显示的是旧的windows2000/XP凭据对话框,而不是新的Vista/7对话框

我在msdn上读到我应该使用这个函数

有人能举个例子说明如何将它与C#一起使用吗?
我还需要能够获取输入的凭据。

以下是一些代码:


我设法实现了一个适合我的解决方案

以下是源代码:

    [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;
    }
我仍然需要解决一些细节问题,比如如何记住最后输入的凭证等等


但是主要部分是有效的。

我看到了这篇文章。问题是我需要提取在对话框中输入的凭据。我认为它需要使用CredUnPackAuthenticationBuffer api调用。我看到您调用了函数
GetCredentialsVistaAndUp
,这对xp也有效还是没有测试?@ChrisjanLodewyks-它对xp不起作用。XP不支持此对话框。它只能在Vista和更高版本上使用,就像方法名所暗示的那样。我不断地得到一个神秘的返回码0x1F/decimal 31。事实证明,我必须为所有内容设置CharSet=CharSet.Unicode,然后它工作得很好。你知道如何获取最后输入的凭据吗?
    [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;
    }