2010-11-09 40 views
9

我想从用户获取网络登录凭据。在C#中为Windows Vista/7显示身份验证对话框

我使用.NET 3.5与C#。 到现在为止我用了CredUIPromptForCredentials呼叫 (一个非常有用的关于如何使用它的链接,可以发现here

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

我读过msdn,我应该使用CredUIPromptForWindowsCredentials函数。

有人可以发表一个如何使用C#的例子吗? 我还需要能够获得输入的凭据。

回答

18

我设法实现一个为我工作的解决方案。

这里是源代码:

[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; 
    } 

我还需要制定出细节,如怎样记住被输入等,最后凭证......

但大部分作品。

+0

我看到你称为你的函数'GetCredentialsVistaAndUp',这个工作是否也适用于XP或者你没有测试过? – 2013-09-25 09:09:04

+1

@ChrisjanLodewyks - 它不适用于XP。 XP不支持这个对话框。它只能在Vista上使用,就像方法名称提示一样。 – codekaizen 2014-01-10 19:33:36

+0

我不断收到一个0x1F/decimal的神秘返回码。事实证明,我必须为所有设置设置CharSet = CharSet.Unicode,然后它工作得很好。 – 2016-06-09 16:48:37

2

下面是一些代码去extracted from bytes.com post

[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 . 
     // ... 
    } 
    } 
} 
+0

我看到这篇文章。问题是我需要提取在对话框中输入的凭证。我认为它需要使用CredUnPackAuthenticationBuffer API调用。 – Rubinsh 2010-11-09 15:04:05

相关问题