public static extern CredUiReturnCodes CredUIPromptForWindowsCredentials( ref CredUiInfo pUiInfo, int dwAuthError, ref uint pulAuthPackage, IntPtr pvInAuthBuffer, uint ulInAuthBufferSize, out IntPtr ppvOutAuthBuffer, out uint pulOutAuthBufferSize, [MarshalAs(UnmanagedType.Bool)] ref bool pfSave, CredUiWinFlags dwFlags);
public static bool PromptForCredentials2( IWin32Window parent, string messageText, string captionText, Win32Error errorCode, ref string domainName, ref string userName, ref string password, ref bool save, CredUiWinFlags flags ) { Win32Error result; CredUiInfo info = new CredUiInfo(); int authenticationPackage = 0; IntPtr outAuthBuffer; int outAuthBufferSize; info.Size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CredUiInfo)); info.Parent = parent != null ? parent.Handle : IntPtr.Zero; info.MessageText = messageText; info.CaptionText = captionText; using (var inAuthBuffer = PackCredentials(0, userName, password)) { result = Win32.CredUIPromptForWindowsCredentials( ref info, errorCode, ref authenticationPackage, inAuthBuffer, inAuthBuffer.Size, out outAuthBuffer, out outAuthBufferSize, ref save, flags ); if (result == Win32Error.Cancelled) return false; if (result != Win32Error.Success) Win32.Throw(result); try { UnpackCredentials( new MemoryRegion(outAuthBuffer, 0, outAuthBufferSize), CredPackFlags.ProtectedCredentials, out domainName, out userName, out password ); return true; } finally { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(outAuthBuffer); } } }
public static bool PromptForCredentials2( IWin32Window parent, string messageText, string captionText, Win32Error errorCode, ref string domainName, ref string userName, ref string password, ref bool save, CredUiWinFlags flags ) { Win32Error result; CredUiInfo info = new CredUiInfo(); int authenticationPackage = 0; IntPtr outAuthBuffer; int outAuthBufferSize; info.Size = CredUiInfo.SizeOf; info.Parent = parent != null ? parent.Handle : IntPtr.Zero; info.MessageText = messageText; info.CaptionText = captionText; using (MemoryRegion inAuthBuffer = PackCredentials(0, userName, password)) { result = Win32.CredUIPromptForWindowsCredentials( ref info, errorCode, ref authenticationPackage, inAuthBuffer, inAuthBuffer.Size, out outAuthBuffer, out outAuthBufferSize, ref save, flags ); if (result == Win32Error.Cancelled) { return(false); } if (result != Win32Error.Success) { Win32.Throw(result); } try { UnpackCredentials( new MemoryRegion(outAuthBuffer, 0, outAuthBufferSize), CredPackFlags.ProtectedCredentials, out domainName, out userName, out password ); return(true); } finally { System.Runtime.InteropServices.Marshal.FreeCoTaskMem(outAuthBuffer); } } }