473,480 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Setting NTFS permissions using 2.0 framework

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
4 14731
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
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
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
amarv
1 New Member
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7995
by: BingoHandJob | last post by:
Hello group! I'm having a problem and I hope some of you may be able to point me in the right direction. I inherited a web site using php, running on Windows 2000 & IIS. The site was...
3
453
by: Pål Andreassen | last post by:
Running Windows 2003 Server Framework 1.1 A site is configured to use integrated security (in IIS 6) Windows autentication and user impersonation in web.config <identity impersonate="true" />...
0
1799
by: Neo | last post by:
Hi Everyone I am trying to set and retrieve NTFS permssions for fileshares on remote machine using ActiveDs Interop. It works for domain groups and users but when try to retrieve permssions for...
9
10854
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
2
6022
by: Joey | last post by:
Hey, I need to change a Share's "Share Permissions" using .NET, and without using WMI, is it possible? Thanks ahead! --Joey
4
4259
by: Ram | last post by:
Hey, I'v managed to find a way of adding NTFS permissions to a certain folder, But the problem is, the folder has a couple of inherited permissions which I want to delete. How can I remove the...
2
313
by: Phil Hey | last post by:
I am trying to set permissions on a folder programmatically, and have success fully used the code below for knowledge base article 266461 : How To: Programmatically Set NTFS File System Folder...
0
1172
by: David Rees | last post by:
Hi I've got a series of NTFS Directories which have permission inheritance unchecked (in Explorer) with a bunch of permissions explicitly stated, and I need to remove these explicit permissions...
0
1031
by: Burns168 | last post by:
Hi all, I have an app that needs to set the NTFS Permissions on registry keys that the app creates and uses. I have searched the web and found a few examples but none seem to work. I have found...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.