I’m trying to create a folder using credentials of a limited admin account supplied by an encrypted .config file- right now my code is running under the assumption that the user has no access to these directories, so an unauthorizedexception is thrown, when given access the code otherwise works, but i can’t do that as it would compromise our security. I know how to get my username / password out of the encrypted file already, i’m just not sure as to what library or syntax i should use to impersonate; this is my code:
//set the cursor
string activeDir = "\\\\department\\shares\\users\\";
//create directory with userID as the folder name
string newPath = System.IO.Path.Combine(activeDir + userID);
System.IO.Directory.CreateDirectory(newPath);
so i need a way to supply credentials but i’m at a loss- i’ve been using System.DirectoryServices.AccountManagement and the pricipalcontext to supply a username/password for making changes to the active directory… do i need to use a similar library to make changes to the file system?
any help would be appreciated, thanks!
I think you could impersonate that user temporarily for the thread performing this action. It seems that this only can be done with P/Invoke. Have a look at this example.
For the sake of completeness (if the link stops working some day), find the
ImpersonatedUserclass below (credits to Jon Cole):