CredUIPromptForWindowsCredentials return junk characters if you P/Invoke it from managed code on OS’s such as Spanish or French.
If you are using StringBuilder for passing in the username, password and domain name to CredUIPromptForWindowsCredentials it might cause this issue.
Pass IntPtr for username, password and domain name as shown in the code snip below.
[DllImport("credui", EntryPoint = "CredUnPackAuthenticationBufferW", CharSet = CharSet.Unicode, SetLastError = true)] internal static extern bool CredUnPackAuthenticationBuffer( uint dwFlags, IntPtr pAuthBuffer, uint cbAuthBuffer, [Out] IntPtr pszUserName, ref uint pcchMaxUserName, [Out] IntPtr pszDomainName, ref uint pcchMaxDomainName, [Out] IntPtr pszPassword, ref uint pcchMaxPassword);
….
uint maxPassword = CREDUI_MAX_PASSWORD_LENGTH; IntPtr passwordBuffer = Marshal.AllocCoTaskMem((int)maxPassword);
uint maxLogonName = CREDUI_MAX_USERNAME_LENGTH; IntPtr logonNameBuffer = Marshal.AllocCoTaskMem((int)maxLogonName);
uint maxDomainNameHint = CREDUI_MAX_USERNAME_LENGTH; IntPtr domainNameHintBuffer = Marshal.AllocCoTaskMem((int)maxDomainNameHint);
…..
if(CredUnPackAuthenticationBuffer( CRED_PACK_PROTECTED_CREDENTIALS, outBuffer, outBufferSize, logonNameBuffer, ref maxLogonName, domainNameHintBuffer, ref maxDomainNameHint, passwordBuffer, ref maxPassword)) { String logonName = Marshal.PtrToStringUni(logonNameBuffer, (int)maxLogonName); Console.WriteLine("User Name: {0}", logonName); } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } |
Reference:
msdn.microsoft.com/en-us/library/windows/desktop/aa375178(v=vs.85).aspx
msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
msdn.microsoft.com/en-us/library/system.intptr.aspx
By Shamik Misra, Escalation Services, Distributed Services