internal static extern int CredUIPromptForWindowsCredentials(ref CredentialUIInfo creditUR, int authError, ref uint authPackage, IntPtr InAuthBuffer, uint InAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool fSave, PromptForWindowsCredentialsFlags flags);
private static extern uint CredUIPromptForWindowsCredentials(ref CreduiInfo notUsedHere, int authError, ref uint authPackage, IntPtr inAuthBuffer, uint inAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool save, PromptForWindowsCredentialsFlags flags);
public static extern CredUIReturnCodes CredUIPromptForWindowsCredentials( ref CREDUI_INFO uiInfo, int authError, ref int authPackage, IntPtr InAuthBuffer, int InAuthBufferSize, out IntPtr refOutAuthBuffer, out int refOutAuthBufferSize, ref bool fSave, PromptForWindowsCredentialsFlags flags);
public static extern int CredUIPromptForWindowsCredentials(ref CREDUI_INFO uiInfo, int authError, ref uint authPackage, IntPtr InAuthBuffer, uint InAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool fSave, PromptForWindowsCredentialsFlags flags);
protected override void EndProcessing() { // Create a new CREDUI_INFO object & set default properties CREDUI_INFO credui = new CREDUI_INFO(); credui.pszCaptionText = "Please enter your credentials"; credui.pszMessageText = ""; credui.cbSize = Marshal.SizeOf(credui); uint authPackage = 0; IntPtr outCredBuffer = new IntPtr(); uint outCredSize; // Instantaniate the credential flags PromptForWindowsCredentialsFlags flags = new PromptForWindowsCredentialsFlags(); // Launch the credential window bool save = false; uint result = CredUIPromptForWindowsCredentials( ref credui, 0, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref save, flags); // Create the buffers 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)) { // Create a new PSCredential SecureString secure = new SecureString(); char[] chars = new char[passwordBuf.Length]; passwordBuf.CopyTo(0, chars, 0, passwordBuf.Length); foreach (char ch in chars) { secure.AppendChar(ch); } credential = new PSCredential(usernameBuf.ToString(), secure); } } else { // Throw an error ErrorRecord error = new ErrorRecord( new Exception("Credentials were not provided or the dialog was closed unexpectedly"), "UnexpectedResult", ErrorCategory.CloseError, result); ThrowTerminatingError(error); } WriteObject(credential); }
private DialogResult ShowDialogAux() { int errorcode = 0; // If zero, displays no message. Otherwise, gives a reason why we are asking for the credentials - what went wrong. uint dialogReturn; uint authPackage = 0; IntPtr outCredBuffer; uint outCredSize; CREDUI_INFO credui = new CREDUI_INFO(); credui.cbSize = Marshal.SizeOf(credui); credui.pszCaptionText = Caption; credui.pszMessageText = Message; credui.hwndParent = ParentForm.Handle; const int ERROR_SUCCESS = 0x00000000; const int ERROR_CANCELLED = 0x000004C7; PromptForWindowsCredentialsFlags flags = (PromptForWindowsCredentialsFlags)0; if (ProvideSaveCheckbox) { flags |= PromptForWindowsCredentialsFlags.CREDUIWIN_CHECKBOX; } flags |= PromptForWindowsCredentialsFlags.CREDUIWIN_GENERIC; //Show the dialog dialogReturn = CredUIPromptForWindowsCredentials(ref credui, errorcode, ref authPackage, (IntPtr)0, //You can force that a specific username is shown in the dialog. Create it with 'CredPackAuthenticationBuffer()'. Then, the buffer goes here... 0, //...and the size goes here. You also have to add CREDUIWIN_IN_CRED_ONLY to the flags (last argument). out outCredBuffer, out outCredSize, ref SaveRequested, 0); //Use the PromptForWindowsCredentialsFlags-Enum here. You can use multiple flags if you seperate them with | . if (dialogReturn != ERROR_SUCCESS) { throw new Exception("Unable to prompt for Windows credentials."); } try { if (dialogReturn == ERROR_CANCELLED) { return(DialogResult.Cancel); } var usernameBuf = new StringBuilder(256); var passwordBuf = new StringBuilder(256); var domainBuf = new StringBuilder(256); for (;;) { int maxUserName = usernameBuf.Capacity; int maxDomain = domainBuf.Capacity; int maxPassword = passwordBuf.Capacity; int dwUnpackFlags = CRED_PACK_PROTECTED_CREDENTIALS; bool Success = CredUnPackAuthenticationBuffer(dwUnpackFlags, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword); if (Success) { Credentials = new NetworkCredential() { UserName = usernameBuf.ToString(), Password = passwordBuf.ToString(), Domain = domainBuf.ToString() }; return(DialogResult.OK); } const int ERROR_INSUFFICIENT_BUFFER = 122; int LastError = Marshal.GetLastWin32Error(); if (LastError == ERROR_INSUFFICIENT_BUFFER) { if (usernameBuf.Capacity >= 65535) { throw new NotSupportedException(); } usernameBuf.EnsureCapacity(Math.Min(65535, usernameBuf.Capacity * 2)); passwordBuf.EnsureCapacity(Math.Min(65535, passwordBuf.Capacity * 2)); domainBuf.EnsureCapacity(Math.Min(65535, domainBuf.Capacity * 2)); } else { throw new Win32Exception(LastError); } } } finally { // TODO: Should call SecureZeroMemory(), but this is a C++ inline function. //clear the memory allocated by CredUIPromptForWindowsCredentials CoTaskMemFree(outCredBuffer); } }