Setting NTFS permissions using 2.0 framework

  • Thread starter Thread starter DavidMGorman
  • Start date Start date
D

DavidMGorman

Apologies if this has been asked & answered (pls post a link if this is
so) but I am tired of finding a close but not quite close enough
solution. I am looking for a sample or explanation of how to use
asp.net 2.0 to set NTFS folder/file permissions. Most of the solutions
I have seen invoke a process that taps into WMI or some WSH script. I
would like to avoid that if possible. If I can be even more restrictive
I would like avoid a COM interop as well. If you know that I must use a
script or interop that would be useful.

Thanks in advance for any help.
 
David,

Have you checked the static GetAccessControl and SetAccessControl
methods on the File and Directory classes in the System.IO namespace? They
will do what you want.

In ASP.NET, you will have to make sure that you have the appropriate
user making the call, since I imagine that the ASPNET account that ASP.NET
runs under doesn't have permissions to modify the rights of anything.

Hope this helps.
 
I found some simple code that does works:

http://www.ftponline.com/special/security/jjarvinen/default_pf.aspx has
a good article.

string sFN = @"c:\spacer.gif"; //pick some target file or
folder

WindowsIdentity self = WindowsIdentity.GetCurrent();
SecurityIdentifier selfSID = self.User; // need to pass in the
SID of the affected user/group

FileSecurity fileSec = System.IO.File.GetAccessControl(sFN);

FileSystemAccessRule fsRule =
new FileSystemAccessRule(selfSID,
FileSystemRights.Read,
AccessControlType.Allow);
// AccessControlType.Deny);

fileSec.AddAccessRule(fsRule);

System.IO.File.SetAccessControl(sFN, fileSec);
 
Back
Top