943,899 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1211
  • C# RSS
Sep 7th, 2009
-1

Code to move file to user-inaccessible directory

Expand Post »
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
Similar Threads
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
stoymigo is offline Offline
52 posts
since Aug 2008
Sep 7th, 2009
0

Re: Code to move file to user-inaccessible directory

C# Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 7th, 2009
0

Re: Code to move file to user-inaccessible directory

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!
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
stoymigo is offline Offline
52 posts
since Aug 2008
Sep 8th, 2009
0

Re: Code to move file to user-inaccessible directory

Hi , the code above worked to move a file to a hidden path
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
stoymigo is offline Offline
52 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: smart cab project does not refresh dependencies
Next Thread in C# Forum Timeline: C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC