在Windows Vista / 7的C#中显示验证对话框
发布时间:2020-12-14 01:41:27 所属栏目:Windows 来源:网络整理
导读:我想从用户获取网络登录凭据. 我正在使用.NET 3.5与C#. 到目前为止,我使用了CredUIPromptForCredentials调用 (一个非常有用的链接,如何使用它可以找到here) 我的问题是,CredUIPromptForCredentials API调用显示旧的Windows 2000 / XP凭据对话框,而不是新的Vi
我想从用户获取网络登录凭据.
我正在使用.NET 3.5与C#. 我的问题是,CredUIPromptForCredentials API调用显示旧的Windows 2000 / XP凭据对话框,而不是新的Vista / 7. 我读过msdn我应该使用CredUIPromptForWindowsCredentials 功能. 有人可以张贴一个如何使用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,ref authPackage,IntPtr.Zero,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; } 我仍然需要找出精细的细节,例如如何记住输入的最后一个凭据等… 但主要工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- crt key转p12, jks p12互转,windows生成jks,
- windows中的/ dev / zero等价?
- windows-server-2008 – 如何从默认域策略中删除文件夹重定
- 在Windows上,应用程序输出可接受的最大文件名长度是多少?
- windows – 如何在不同的子网中设置VPN
- .net – Windows Phone 7:不支持现有库?
- 在R中创建一组同步的Windows 7 PC并行处理?
- windows – 通过Ruby持久化环境变量
- windows – R markdown v2 to pdf.当绘图中的非拉丁字符时转
- 如何判断win32 c应用程序在CTRL-ALT-DEL之后是否失去焦点?