Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4.  
  5. [module: DefaultCharSet(CharSet.Unicode)]
  6.  
  7. public static class CredentialPrompt
  8. {
  9. public readonly struct Result
  10. {
  11. public Result(string userName, string domain, string password)
  12. {
  13. UserName = userName;
  14. Domain = domain;
  15. Password = password;
  16. }
  17.  
  18. public string UserName { get; }
  19. public string Domain { get; }
  20. public string Password { get; }
  21. }
  22.  
  23. public static Result? ShowDialog(HandleRef owner, string caption, string message, bool previousAttemptFailed)
  24. {
  25. var authPackage = (uint)0;
  26. var r = CredUIPromptForWindowsCredentials(
  27. new CREDUI_INFO(owner.Handle, message, caption),
  28. previousAttemptFailed ? (uint)WinErrorCode.LogonFailure : 0,
  29. ref authPackage,
  30. pvInAuthBuffer: IntPtr.Zero,
  31. ulInAuthBufferSize: 0,
  32. out var packedResultBuffer,
  33. out var packedResultBufferSize,
  34. pfSave: IntPtr.Zero,
  35. CREDUIWIN.GENERIC);
  36.  
  37. using (packedResultBuffer)
  38. {
  39. switch ((WinErrorCode)r)
  40. {
  41. case WinErrorCode.Cancelled:
  42. return null;
  43. case WinErrorCode.Success:
  44. break;
  45. default:
  46. throw new Win32Exception(r);
  47. }
  48.  
  49. unsafe
  50. {
  51. var userNameBufferSize = 128 * sizeof(char);
  52. var userNameBuffer = new char[userNameBufferSize];
  53. var domainBufferSize = 128 * sizeof(char);
  54. var domainBuffer = new char[domainBufferSize];
  55. var passwordBufferSize = 128 * sizeof(char);
  56. var passwordBuffer = new char[passwordBufferSize];
  57. fixed (char* usernamePointer = userNameBuffer)
  58. fixed (char* domainPointer = domainBuffer)
  59. fixed (char* passwordPointer = passwordBuffer)
  60. {
  61. try
  62. {
  63. if (!CredUnPackAuthenticationBuffer(CRED_PACK.GENERIC_CREDENTIALS, packedResultBuffer, packedResultBufferSize, usernamePointer, ref userNameBufferSize, domainPointer, ref domainBufferSize, passwordPointer, ref passwordBufferSize))
  64. throw new Win32Exception();
  65.  
  66. GC.KeepAlive(owner.Wrapper);
  67.  
  68. return new Result(
  69. new string(usernamePointer),
  70. new string(domainPointer),
  71. new string(passwordPointer));
  72. }
  73. finally
  74. {
  75. Array.Clear(passwordBuffer, 0, passwordBuffer.Length);
  76. }
  77. }
  78. }
  79. }
  80. }
  81.  
  82. #pragma warning disable IDE0052 // Remove unread private members
  83.  
  84. private enum WinErrorCode : ushort
  85. {
  86. Success = 0,
  87. Cancelled = 1223,
  88. LogonFailure = 1326
  89. }
  90.  
  91. [DllImport("credui.dll", SetLastError = true)]
  92. private static extern unsafe bool CredUnPackAuthenticationBuffer(CRED_PACK dwFlags, CoTaskMemSafeHandle pAuthBuffer, uint cbAuthBuffer, char* pszUserName, ref int pcchMaxUserName, char* pszDomainName, ref int pcchMaxDomainame, char* pszPassword, ref int pcchMaxPassword);
  93.  
  94. private sealed class CoTaskMemSafeHandle : SafeHandle
  95. {
  96. private CoTaskMemSafeHandle()
  97. : base(invalidHandleValue: IntPtr.Zero, ownsHandle: true)
  98. {
  99. }
  100.  
  101. public override bool IsInvalid => handle == IntPtr.Zero;
  102.  
  103. protected override bool ReleaseHandle()
  104. {
  105. Marshal.FreeCoTaskMem(handle);
  106. return true;
  107. }
  108. }
  109.  
  110. private enum CRED_PACK : uint
  111. {
  112. PROTECTED_CREDENTIALS = 0x1,
  113. WOW_BUFFER = 0x2,
  114. GENERIC_CREDENTIALS = 0x4,
  115. ID_PROVIDER_CREDENTIALS = 0x8
  116. }
  117.  
  118. private readonly struct CREDUI_INFO
  119. {
  120. private readonly int cbSize;
  121. private readonly IntPtr hwndParent;
  122. private readonly string pszMessageText;
  123. private readonly string pszCaptionText;
  124. private readonly IntPtr hbmBanner;
  125.  
  126. public CREDUI_INFO(IntPtr hwndParent, string pszMessageText, string pszCaptionText)
  127. {
  128. cbSize = Marshal.SizeOf<CREDUI_INFO>();
  129. this.hwndParent = hwndParent;
  130. this.pszMessageText = pszMessageText;
  131. this.pszCaptionText = pszCaptionText;
  132. hbmBanner = IntPtr.Zero;
  133. }
  134. }
  135.  
  136. [DllImport("credui.dll")]
  137. private static extern int CredUIPromptForWindowsCredentials(in CREDUI_INFO pUiInfo, uint dwAuthError, ref uint pulAuthPackage, IntPtr pvInAuthBuffer, uint ulInAuthBufferSize, out CoTaskMemSafeHandle ppvOutAuthBuffer, out uint pulOutAuthBufferSize, IntPtr pfSave, CREDUIWIN dwFlags);
  138.  
  139. private enum CREDUIWIN : uint
  140. {
  141. GENERIC = 0x00000001,
  142. CHECKBOX = 0x00000002,
  143. AUTHPACKAGE_ONLY = 0x00000010,
  144. IN_CRED_ONLY = 0x00000020,
  145. ENUMERATE_ADMINS = 0x00000100,
  146. ENUMERATE_CURRENT_USER = 0x00000200,
  147. SECURE_PROMPT = 0x00001000,
  148. PREPROMPTING = 0x00002000,
  149. PACK_32_WOW = 0x10000000
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement