Code to move file to user-inaccessible directory

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 48
Reputation: stoymigo is an unknown quantity at this point 
Solved Threads: 1
stoymigo stoymigo is offline Offline
Light Poster

Code to move file to user-inaccessible directory

 
-1
  #1
Sep 7th, 2009
Hi,
we are running a windows server 2003 os on our network.
The application i develop needs to be able to move files to folder
that the user running the app can't access.

The application already stores the user details of the administrator , how can i use those details(username,password and the network) to enable a basic user to move files to hidden path.

-Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,251
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 578
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Code to move file to user-inaccessible directory

 
0
  #2
Sep 7th, 2009
  1. using System.Runtime.InteropServices; // DllImport
  2. using System.Security.Principal; // WindowsImpersonationContext
  3. using System.Security.Permissions; // PermissionSetAttribute
  4. ...
  5.  
  6. public WindowsImpersonationContext
  7. ImpersonateUser(string sUsername, string sDomain, string sPassword)
  8. {
  9. // initialize tokens
  10. IntPtr pExistingTokenHandle = new IntPtr(0);
  11. IntPtr pDuplicateTokenHandle = new IntPtr(0);
  12. pExistingTokenHandle = IntPtr.Zero;
  13. pDuplicateTokenHandle = IntPtr.Zero;
  14.  
  15. // if domain name was blank, assume local machine
  16. if (sDomain == "")
  17. sDomain = System.Environment.MachineName;
  18.  
  19. try
  20. {
  21. string sResult = null;
  22.  
  23. const int LOGON32_PROVIDER_DEFAULT = 0;
  24.  
  25. // create token
  26. const int LOGON32_LOGON_INTERACTIVE = 2;
  27. //const int SecurityImpersonation = 2;
  28.  
  29. // get handle to token
  30. bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,
  31. LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
  32. ref pExistingTokenHandle);
  33.  
  34. // did impersonation fail?
  35. if (false == bImpersonated)
  36. {
  37. int nErrorCode = Marshal.GetLastWin32Error();
  38. sResult = "LogonUser() failed with error code: " +
  39. nErrorCode + "\r\n";
  40.  
  41. // show the reason why LogonUser failed
  42. MessageBox.Show(this, sResult, "Error",
  43. MessageBoxButtons.OK, MessageBoxIcon.Error);
  44. }
  45.  
  46. // Get identity before impersonation
  47. sResult += "Before impersonation: " +
  48. WindowsIdentity.GetCurrent().Name + "\r\n";
  49.  
  50. bool bRetVal = DuplicateToken(pExistingTokenHandle,
  51. (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
  52. ref pDuplicateTokenHandle);
  53.  
  54. // did DuplicateToken fail?
  55. if (false == bRetVal)
  56. {
  57. int nErrorCode = Marshal.GetLastWin32Error();
  58. // close existing handle
  59. CloseHandle(pExistingTokenHandle);
  60. sResult += "DuplicateToken() failed with error code: "
  61. + nErrorCode + "\r\n";
  62.  
  63. // show the reason why DuplicateToken failed
  64. MessageBox.Show(this, sResult, "Error",
  65. MessageBoxButtons.OK, MessageBoxIcon.Error);
  66. return null;
  67. }
  68. else
  69. {
  70. // create new identity using new primary token
  71. WindowsIdentity newId = new WindowsIdentity
  72. (pDuplicateTokenHandle);
  73. WindowsImpersonationContext impersonatedUser =
  74. newId.Impersonate();
  75.  
  76. // check the identity after impersonation
  77. sResult += "After impersonation: " +
  78. WindowsIdentity.GetCurrent().Name + "\r\n";
  79.  
  80. MessageBox.Show(this, sResult, "Success",
  81. MessageBoxButtons.OK, MessageBoxIcon.Information);
  82. return impersonatedUser;
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. throw ex;
  88. }
  89. finally
  90. {
  91. // close handle(s)
  92. if (pExistingTokenHandle != IntPtr.Zero)
  93. CloseHandle(pExistingTokenHandle);
  94. if (pDuplicateTokenHandle != IntPtr.Zero)
  95. CloseHandle(pDuplicateTokenHandle);
  96. }
  97. }

Borrowed from:
http://www.codeproject.com/KB/cs/cpimpersonation1.aspx
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 48
Reputation: stoymigo is an unknown quantity at this point 
Solved Threads: 1
stoymigo stoymigo is offline Offline
Light Poster

Re: Code to move file to user-inaccessible directory

 
0
  #3
Sep 7th, 2009
Hi , the code that you use is right.
I will test it tomorrow , with an administrator's login details to see whether the code i execute would execute as it would if he/she is logged in.

I had an exception thrown (using my own id) ,
"DirectoryNotFoundException " , and on the codeproject discussions it shows that you must use UNC path and it worked.

If works i will mark as solved , thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 48
Reputation: stoymigo is an unknown quantity at this point 
Solved Threads: 1
stoymigo stoymigo is offline Offline
Light Poster

Re: Code to move file to user-inaccessible directory

 
0
  #4
Sep 8th, 2009
Hi , the code above worked to move a file to a hidden path
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC