Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Principal;
  4. using System.Security.Permissions;
  5. using Microsoft.Win32.SafeHandles;
  6. using System.Runtime.ConstrainedExecution;
  7. using System.Security;
  8.  
  9. namespace FetchFile
  10. {
  11. class Program
  12. {
  13. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  14. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
  15. int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
  16.  
  17. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  18. public extern static bool CloseHandle(IntPtr handle);
  19.  
  20. [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
  21. public static void Main(string[] args)
  22. {
  23. SafeTokenHandle safeTokenHandle;
  24. try
  25. {
  26. string userName, domainName, password;
  27. domainName = "domain";
  28. userName = "username";
  29. password = "password";
  30. const int LOGON32_PROVIDER_WINNT50 = 3;
  31. const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
  32.  
  33. bool returnValue = LogonUser(userName, domainName, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, out safeTokenHandle);
  34.  
  35. Console.WriteLine("LogonUser called.");
  36.  
  37. if (false == returnValue)
  38. {
  39. int ret = Marshal.GetLastWin32Error();
  40. Console.WriteLine("LogonUser failed with error code : {0}", ret);
  41. throw new System.ComponentModel.Win32Exception(ret);
  42. }
  43. using (safeTokenHandle)
  44. {
  45. Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
  46. Console.WriteLine("Value of Windows NT token: " + safeTokenHandle);
  47.  
  48. Console.WriteLine("Before impersonation: "
  49. + WindowsIdentity.GetCurrent().Name);
  50.  
  51.  
  52. using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
  53. {
  54.  
  55. string src = @"\serversharedfile.txt";
  56. string dest = @"c:file.txt";
  57. File.Copy(dest, src);
  58.  
  59. Console.WriteLine("After impersonation: "+ WindowsIdentity.GetCurrent().Name);
  60. }
  61. Console.WriteLine("After closing the context: " + WindowsIdentity.GetCurrent().Name);
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine("Exception occurred. " + ex.Message);
  67. }
  68.  
  69. }
  70.  
  71. }
  72. }
  73.  
  74. public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
  75. {
  76. private SafeTokenHandle()
  77. : base(true)
  78. {
  79. }
  80.  
  81. [DllImport("kernel32.dll")]
  82. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  83. [SuppressUnmanagedCodeSecurity]
  84. [return: MarshalAs(UnmanagedType.Bool)]
  85. private static extern bool CloseHandle(IntPtr handle);
  86.  
  87. protected override bool ReleaseHandle()
  88. {
  89. return CloseHandle(handle);
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement