2010-11-09 42 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

相關問題