在C#中显示身份验证对话框Vista / 7的窗口 [英] Show Authentication dialog in C# for windows Vista/7
问题描述
我想从用户获得网络的登录凭据。
I want to get network login credentials from a user.
我使用.NET 3.5 C#。
到现在为止,我使用了 CredUIPromptForCredentials
通话
(关于如何使用它非常有用的连结,可以发现<一href=\"http://stackoverflow.com/questions/1596161/how-to-show-authentication-dialog-in-c-net-3-5-sp1/1596411\">here)
I'm using .NET 3.5 with C#.
Up until now I used the CredUIPromptForCredentials
call
(a very useful link on how to use it can be found here)
我的问题是, CredUIPromptForCredentials
API调用显示旧的Windows 2000 / XP凭据对话框,而不是新的Vista / 7之一。
My problem is that the CredUIPromptForCredentials
API call shows the old windows 2000/XP credentials dialog and not the new Vista/7 one.
我读过的,我应该使用 CredUIPromptForWindowsCredentials 功能MSDN。
I've read on msdn that I should use the CredUIPromptForWindowsCredentials function.
有人可以张贴的如何在C#中使用它的例子吗?
我还需要能够获得中输入的凭据。
Can someone post an example of how to use it with C#? I also need to be able to get the credentials that were entered.
推荐答案
我成功地实现了为我工作的解决方案。
I managed to implement a solution that is working for me.
下面是源$ C $ C:
Here is the source code:
[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;
}
我还需要制定出细节,如怎样记住所输入等最后的凭据...
I still need to work out the fine details such as how to remember the last credentials that were entered etc...
但主要部分的工作。
这篇关于在C#中显示身份验证对话框Vista / 7的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!