private static void GetCredentialsFromOutputBuffer(ref string user, ref string password, ref string domain, IntPtr outCredBuffer, uint outCredSize) { int maxUserName = 100; int maxDomain = 100; int maxPassword = 100; var usernameBuf = new StringBuilder(maxUserName); var passwordBuf = new StringBuilder(maxDomain); var domainBuf = new StringBuilder(maxPassword); if (NativeCode.CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword)) { user = usernameBuf.ToString(); password = passwordBuf.ToString(); domain = domainBuf.ToString(); if (string.IsNullOrWhiteSpace(domain)) { Debug.WriteLine("Domain null"); if (!ParseUserName(usernameBuf.ToString(), usernameBuf.Capacity, domainBuf.Capacity, out user, out domain)) { user = usernameBuf.ToString(); } password = passwordBuf.ToString(); } } //mimic SecureZeroMem function to make sure buffer is zeroed out. SecureZeroMem is not an exported function, neither is RtlSecureZeroMemory var zeroBytes = new byte[outCredSize]; Marshal.Copy(zeroBytes, 0, outCredBuffer, (int)outCredSize); //clear the memory allocated by CredUIPromptForWindowsCredentials NativeCode.CoTaskMemFree(outCredBuffer); }
private static bool PromptForCredentials(string target, NativeCode.CredentialUIInfo credUI, ref bool save, ref string user, out string password, out string domain) { password = string.Empty; domain = string.Empty; // Setup the flags and variables credUI.cbSize = Marshal.SizeOf(credUI); int errorcode = 0; uint authPackage = 0; var outCredBuffer = new IntPtr(); uint outCredSize; var flags = NativeCode.PromptForWindowsCredentialsFlags.GenericCredentials | NativeCode.PromptForWindowsCredentialsFlags.EnumerateCurrentUser; flags = save ? flags | NativeCode.PromptForWindowsCredentialsFlags.ShowCheckbox : flags; // Prefill username IntPtr inCredBuffer; int inCredSize; GetInputBuffer(user, out inCredBuffer, out inCredSize); // Setup the flags and variables int result = NativeCode.CredUIPromptForWindowsCredentials(ref credUI, errorcode, ref authPackage, inCredBuffer, inCredSize, out outCredBuffer, out outCredSize, ref save, flags); if (inCredBuffer != IntPtr.Zero) { NativeCode.CoTaskMemFree(inCredBuffer); } if (result == 0) { GetCredentialsFromOutputBuffer(ref user, ref password, ref domain, outCredBuffer, outCredSize); return(true); } user = null; domain = null; return(false); }
private static void GetInputBuffer(string user, out IntPtr inCredBuffer, out int inCredSize) { if (!string.IsNullOrEmpty(user)) { var usernameBuf = new StringBuilder(user); var passwordBuf = new StringBuilder(); inCredSize = 1024; inCredBuffer = Marshal.AllocCoTaskMem(inCredSize); if (NativeCode.CredPackAuthenticationBuffer(0x00, usernameBuf, passwordBuf, inCredBuffer, ref inCredSize)) { return; } if (inCredBuffer != IntPtr.Zero) { NativeCode.CoTaskMemFree(inCredBuffer); } } inCredBuffer = IntPtr.Zero; inCredSize = 0; }
private static bool PromptForCredentials(string target, NativeCode.CredentialUIInfo credUI, ref bool save, out string user, out string password, out string domain) { user = String.Empty; password = String.Empty; domain = String.Empty; // Setup the flags and variables credUI.cbSize = Marshal.SizeOf(credUI); int errorcode = 0; uint authPackage = 0; IntPtr outCredBuffer = new IntPtr(); uint outCredSize; var flags = NativeCode.PromptForWindowsCredentialsFlags.GenericCredentials | NativeCode.PromptForWindowsCredentialsFlags.EnumerateCurrentUser; flags = save ? flags | NativeCode.PromptForWindowsCredentialsFlags.ShowCheckbox : flags; // Setup the flags and variables int result = NativeCode.CredUIPromptForWindowsCredentials(ref credUI, errorcode, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref save, flags); 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 (NativeCode.CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword)) { user = usernameBuf.ToString(); password = passwordBuf.ToString(); domain = domainBuf.ToString(); if (String.IsNullOrWhiteSpace(domain)) { Debug.WriteLine("Domain null"); if (!ParseUserName(usernameBuf.ToString(), usernameBuf.Capacity, domainBuf.Capacity, out user, out domain)) { user = usernameBuf.ToString(); } password = passwordBuf.ToString(); } } //mimic SecureZeroMem function to make sure buffer is zeroed out. SecureZeroMem is not an exported function, neither is RtlSecureZeroMemory var zeroBytes = new byte[outCredSize]; Marshal.Copy(zeroBytes, 0, outCredBuffer, (int)outCredSize); //clear the memory allocated by CredUIPromptForWindowsCredentials NativeCode.CoTaskMemFree(outCredBuffer); return(true); } user = null; domain = null; return(false); }