Guest User

Untitled

a guest
May 3rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security;
  4. using Misuzilla.Security;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. PromptCredentialsResult cred = null;
  13. PromptCredentialsSecureStringResult credSecure = null;
  14. var defaultUserName = "Uiharu Kazari".ToSecureString();
  15.  
  16. //credSecure = CredentialUI.PromptForWindowsCredentialsWithSecureString("Hauhau", "maumau");
  17. credSecure = CredentialUI.PromptWithSecureString("キャプション", "メッセージ", defaultUserName, null);
  18. //cred = CredentialUI.Prompt("キャプション", "メッセージ");
  19.  
  20. if (credSecure != null)
  21. {
  22. String userNameRaw = credSecure.UserName.ToInsecureString();
  23. String passwordRaw = credSecure.Password.ToInsecureString();
  24. Console.WriteLine("UserName: {0}", userNameRaw);
  25. Console.WriteLine("Password: {0}", passwordRaw);
  26. }
  27. if (cred != null)
  28. {
  29. Console.WriteLine("UserName: {0}", cred.UserName);
  30. Console.WriteLine("Password: {0}", cred.Password);
  31. }
  32. Console.WriteLine("Push any key...");
  33. Console.Read();
  34. }
  35.  
  36. }
  37.  
  38. public static class Extension
  39. {
  40. public static SecureString ToSecureString(this String s)
  41. {
  42. SecureString ss = new SecureString();
  43. foreach (var c in s)
  44. ss.AppendChar(c);
  45. ss.MakeReadOnly();
  46. return ss;
  47. }
  48.  
  49. public static String ToInsecureString(this SecureString s)
  50. {
  51. IntPtr p = IntPtr.Zero;
  52. try
  53. {
  54. p = Marshal.SecureStringToCoTaskMemUnicode(s);
  55. return Marshal.PtrToStringUni(p);
  56. }
  57. finally
  58. {
  59. if (p != IntPtr.Zero)
  60. Marshal.ZeroFreeCoTaskMemUnicode(p);
  61. }
  62. }
  63. public static SecureString PtrToSecureString(IntPtr p)
  64. {
  65. SecureString s = new SecureString();
  66. Int32 i = 0;
  67. while (true)
  68. {
  69. Char c = (Char)Marshal.ReadInt16(p, ((i++) * sizeof(Int16)));
  70. if (c == '\u0000')
  71. break;
  72. s.AppendChar(c);
  73. }
  74. s.MakeReadOnly();
  75. return s;
  76. }
  77. public static SecureString PtrToSecureString(IntPtr p, Int32 length)
  78. {
  79. SecureString s = new SecureString();
  80. for (var i = 0; i < length; i++)
  81. s.AppendChar((Char)Marshal.ReadInt16(p, i * sizeof(Int16)));
  82. s.MakeReadOnly();
  83. return s;
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment