using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace CredentialsPrompt { internal class CredentialsPromptHelper { [DllImport("ole32.dll")] internal static extern void CoTaskMemFree(IntPtr ptr); [DllImport("credui.dll", CharSet = CharSet.Unicode)] internal static extern uint CredUIPromptForWindowsCredentials(ref CREDUI_INFO notUsedHere, int authError, ref uint authPackage, IntPtr InAuthBuffer, uint InAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool fSave, PromptForWindowsCredentialsFlags flags); [DllImport("credui.dll", CharSet = CharSet.Auto)] internal static extern bool CredUnPackAuthenticationBuffer(int dwFlags, IntPtr pAuthBuffer, uint cbAuthBuffer, StringBuilder pszUserName, ref int pcchMaxUserName, StringBuilder pszDomainName, ref int pcchMaxDomainname, StringBuilder pszPassword, ref int pcchMaxPassword); [DllImport("user32.dll")] internal static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa); [DllImport("user32.dll")] internal static extern bool SwitchDesktop(IntPtr hDesktop); [DllImport("user32.dll")] internal static extern bool CloseDesktop(IntPtr handle); [DllImport("user32.dll")] internal static extern bool SetThreadDesktop(IntPtr hDesktop); [DllImport("user32.dll")] internal static extern IntPtr GetThreadDesktop(int dwThreadId); [DllImport("kernel32.dll")] internal static extern int GetCurrentThreadId(); internal enum DESKTOP_ACCESS : uint { DESKTOP_NONE = 0, DESKTOP_READOBJECTS = 0x0001, DESKTOP_CREATEWINDOW = 0x0002, DESKTOP_CREATEMENU = 0x0004, DESKTOP_HOOKCONTROL = 0x0008, DESKTOP_JOURNALRECORD = 0x0010, DESKTOP_JOURNALPLAYBACK = 0x0020, DESKTOP_ENUMERATE = 0x0040, DESKTOP_WRITEOBJECTS = 0x0080, DESKTOP_SWITCHDESKTOP = 0x0100, GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP) } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct CREDUI_INFO { public int cbSize; public IntPtr hwndParent; public string pszMessageText; public string pszCaptionText; public IntPtr hbmBanner; } [Flags] internal enum PromptForWindowsCredentialsFlags { /// /// The caller is requesting that the credential provider return the user name and password in plain text. /// This value cannot be combined with SECURE_PROMPT. /// CREDUIWIN_GENERIC = 0x1, /// /// The Save check box is displayed in the dialog box. /// CREDUIWIN_CHECKBOX = 0x2, /// /// Only credential providers that support the authentication package specified by the authPackage parameter should be enumerated. /// This value cannot be combined with CREDUIWIN_IN_CRED_ONLY. /// CREDUIWIN_AUTHPACKAGE_ONLY = 0x10, /// /// Only the credentials specified by the InAuthBuffer parameter for the authentication package specified by the authPackage parameter should be enumerated. /// If this flag is set, and the InAuthBuffer parameter is NULL, the function fails. /// This value cannot be combined with CREDUIWIN_AUTHPACKAGE_ONLY. /// CREDUIWIN_IN_CRED_ONLY = 0x20, /// /// Credential providers should enumerate only administrators. This value is intended for User Account Control (UAC) purposes only. We recommend that external callers not set this flag. /// CREDUIWIN_ENUMERATE_ADMINS = 0x100, /// /// Only the incoming credentials for the authentication package specified by the authPackage parameter should be enumerated. /// CREDUIWIN_ENUMERATE_CURRENT_USER = 0x200, /// /// The credential dialog box should be displayed on the secure desktop. This value cannot be combined with CREDUIWIN_GENERIC. /// Windows Vista: This value is not supported until Windows Vista with SP1. /// CREDUIWIN_SECURE_PROMPT = 0x1000, /// /// The credential provider should align the credential BLOB pointed to by the refOutAuthBuffer parameter to a 32-bit boundary, even if the provider is running on a 64-bit system. /// CREDUIWIN_PACK_32_WOW = 0x10000000, } } } public class Credentials { public string Username { get; set; } public string Password { get; set; } }