By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Setting NTFS permissions using 2.0 framework

P: n/a
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.

Jun 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DMG
I found some simple code that does works:

http://www.ftponline.com/special/sec...efault_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);

Jun 27 '06 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Da**********@gmail.com> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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.

Jun 27 '06 #3

P: n/a
DMG
Thanks!

I have found this
http://msdn2.microsoft.com/en-us/library/23c9e959.aspx the
DirectorySecurity Class which was really helpful.

/dg

Jun 27 '06 #4

P: 1
Hi David,

You can use System.Security.AccessControl namespace and DirectorySecurity Class to implement the NTFS permissions.

Please look into the following site:
http://msdn2.microsoft.com/en-us/library/ms277579.aspx
http://msdn2.microsoft.com/en-us/library/23c9e959.aspx


sample code:
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
class DirectoryExample
{
public static void Main()
{
try
{
string DirectoryName = "TestDirectory";

Console.WriteLine("Adding access control entry for " + DirectoryName);

// Add the access control entry to the directory.
AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Removing access control entry from " + DirectoryName);

// Remove the access control entry from the directory.
RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}

Console.ReadLine();
}

// Adds an ACL entry on the specified directory for the specified account.
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);

// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));

// Set the new access settings.
dInfo.SetAccessControl(dSecurity);

}

// Removes an ACL entry on the specified directory for the specified account.
public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);

// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings.
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));

// Set the new access settings.
dInfo.SetAccessControl(dSecurity);

}
}
}



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.
Jul 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.