| | |
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:
Solved Threads: 1
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
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
C# Syntax (Toggle Plain Text)
using System.Runtime.InteropServices; // DllImport using System.Security.Principal; // WindowsImpersonationContext using System.Security.Permissions; // PermissionSetAttribute ... public WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword) { // initialize tokens IntPtr pExistingTokenHandle = new IntPtr(0); IntPtr pDuplicateTokenHandle = new IntPtr(0); pExistingTokenHandle = IntPtr.Zero; pDuplicateTokenHandle = IntPtr.Zero; // if domain name was blank, assume local machine if (sDomain == "") sDomain = System.Environment.MachineName; try { string sResult = null; const int LOGON32_PROVIDER_DEFAULT = 0; // create token const int LOGON32_LOGON_INTERACTIVE = 2; //const int SecurityImpersonation = 2; // get handle to token bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); // did impersonation fail? if (false == bImpersonated) { int nErrorCode = Marshal.GetLastWin32Error(); sResult = "LogonUser() failed with error code: " + nErrorCode + "\r\n"; // show the reason why LogonUser failed MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Get identity before impersonation sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n"; bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle); // did DuplicateToken fail? if (false == bRetVal) { int nErrorCode = Marshal.GetLastWin32Error(); // close existing handle CloseHandle(pExistingTokenHandle); sResult += "DuplicateToken() failed with error code: " + nErrorCode + "\r\n"; // show the reason why DuplicateToken failed MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } else { // create new identity using new primary token WindowsIdentity newId = new WindowsIdentity (pDuplicateTokenHandle); WindowsImpersonationContext impersonatedUser = newId.Impersonate(); // check the identity after impersonation sResult += "After impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n"; MessageBox.Show(this, sResult, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); return impersonatedUser; } } catch (Exception ex) { throw ex; } finally { // close handle(s) if (pExistingTokenHandle != IntPtr.Zero) CloseHandle(pExistingTokenHandle); if (pDuplicateTokenHandle != IntPtr.Zero) CloseHandle(pDuplicateTokenHandle); } }
Borrowed from:
http://www.codeproject.com/KB/cs/cpimpersonation1.aspx
•
•
Join Date: Aug 2008
Posts: 48
Reputation:
Solved Threads: 1
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!
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!
![]() |
Similar Threads
- user writable directory (Game Development)
- file manipulation (C#)
- Encrypt File using User Supplied Password (VB.NET)
- uploaded file moving to new directory (PHP)
- File handling: How to search and replace strings in file using user input (C)
- replace data of a text file with user input (C++)
- how to move a single file from a folder (Java)
- How To Make .dbf File Extarction User Friendly (Visual Basic 4 / 5 / 6)
- Determine in which file a user function is defined? (PHP)
- Moving a file location (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: smart cab project does not refresh dependencies
- Next Thread: C#
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# cast check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sockets sql sql-server statistics stream string stringformatting sun table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






