在Windows Vista / 7的C#中显示验证对话框

前端之家收集整理的这篇文章主要介绍了在Windows Vista / 7的C#中显示验证对话框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从用户获取网络登录凭据.

我正在使用.NET 3.5与C#.
到目前为止,我使用了CredUIPromptForCredentials调用
(一个非常有用的链接,如何使用它可以找到here)

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

我读过msdn我应该使用@L_404_1@功能.

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

我设法实施一个为我工作的解决方案.

这是源代码

  1. [DllImport("ole32.dll")]
  2. public static extern void CoTaskMemFree(IntPtr ptr);
  3.  
  4. [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Auto)]
  5. private struct CREDUI_INFO
  6. {
  7. public int cbSize;
  8. public IntPtr hwndParent;
  9. public string pszMessageText;
  10. public string pszCaptionText;
  11. public IntPtr hbmBanner;
  12. }
  13.  
  14.  
  15. [DllImport("credui.dll",CharSet = CharSet.Auto)]
  16. 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);
  17.  
  18. [DllImport("credui.dll",CharSet = CharSet.Auto)]
  19. 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);
  20.  
  21.  
  22.  
  23. public static void GetCredentialsVistaAndUp(string serverName,out NetworkCredential networkCredential)
  24. {
  25. CREDUI_INFO credui = new CREDUI_INFO();
  26. credui.pszCaptionText = "Please enter the credentails for " + serverName;
  27. credui.pszMessageText = "DisplayedMessage";
  28. credui.cbSize = Marshal.SizeOf(credui);
  29. uint authPackage = 0;
  30. IntPtr outCredBuffer = new IntPtr();
  31. uint outCredSize;
  32. bool save = false;
  33. int result = CredUIPromptForWindowsCredentials(ref credui,ref authPackage,IntPtr.Zero,out outCredBuffer,out outCredSize,ref save,1 /* Generic */);
  34.  
  35. var usernameBuf = new StringBuilder(100);
  36. var passwordBuf = new StringBuilder(100);
  37. var domainBuf = new StringBuilder(100);
  38.  
  39. int maxUserName = 100;
  40. int maxDomain = 100;
  41. int maxPassword = 100;
  42. if (result == 0)
  43. {
  44. if (CredUnPackAuthenticationBuffer(0,outCredBuffer,outCredSize,usernameBuf,ref maxUserName,domainBuf,ref maxDomain,passwordBuf,ref maxPassword))
  45. {
  46. //TODO: ms documentation says we should call this but i can't get it to work
  47. //SecureZeroMem(outCredBuffer,outCredSize);
  48.  
  49. //clear the memory allocated by CredUIPromptForWindowsCredentials
  50. CoTaskMemFree(outCredBuffer);
  51. networkCredential = new NetworkCredential()
  52. {
  53. UserName = usernameBuf.ToString(),Password = passwordBuf.ToString(),Domain = domainBuf.ToString()
  54. };
  55. return;
  56. }
  57. }
  58.  
  59. networkCredential = null;
  60. }

我仍然需要找出精细的细节,例如如何记住输入的最后一个凭据等…

但主要工作.

猜你在找的Windows相关文章