This step-by-step article describes how to programmatically
set NTFS file system folder permissions by using Active Directory Service
Interfaces (ADSI) in Microsoft Visual C#.
Build the sample application
To run the following sample application, you must have the
ADsSecurity.dll file and the ADsSecurity.dll file installed. These files are
included with the software development kit (SDK) for Active Directory Service
Interfaces 2.5. To download the SDK for Active Directory Service Interfaces
2.5, visit the following Microsoft Web site:
Note To run the sample application, you must have administrative
credentials on the computer.
To build the sample application, follow
these steps:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, click
New, and then click Project.
- In Visual C# Projects, click
Windows Application under Templates.
Note In Visual Studio 2005, Visual C# Projects is changed to Visual C#. - In the Name box, type
NTFSPermissions, and then click
OK.
- Add a Button control to the Form1
form.
- On the Project menu, click Add
Reference.
- Click the COM tab, click to select the
following items, and then click OK:
- Active DS Type Library
- ADsSecurity 2.5 Type
Library
- Right-click the Form1 form, and then click
View Code.
- Add the following using statements to the top of the source code in the Form1 form.
using ADSSECURITYLib;
using ActiveDs;
- Add the following method to the Form1 class.
public void SetPermissions(String vPath, String UserName )
{
ADsSecurity objADsSec;
SecurityDescriptor objSecDes;
AccessControlList objDAcl;
AccessControlEntry objAce1;
AccessControlEntry objAce2;
Object objSIdHex;
ADsSID objSId;
objADsSec = new ADsSecurityClass();
objSecDes = (SecurityDescriptor) (objADsSec.GetSecurityDescriptor("FILE://" + vPath));
objDAcl = (AccessControlList)objSecDes.DiscretionaryAcl;
objSId = new ADsSIDClass();
objSId.SetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SAM, UserName.ToString());
objSIdHex = objSId.GetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SDDL);
// Add a new access control entry (ACE) object (objAce) so that the user has Full Control permissions on NTFS file system files.
objAce1 = new AccessControlEntryClass();
objAce1.Trustee = (objSIdHex).ToString();
objAce1.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL;
objAce1.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
objAce1.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ONLY_ACE | 1;
objDAcl.AddAce(objAce1);
// Add a new access control entry object (objAce) so that the user has Full Control permissions on NTFS file system folders.
objAce2 = new AccessControlEntryClass();
objAce2.Trustee = (objSIdHex).ToString();
objAce2.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL;
objAce2.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
objAce2.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | 1;
objDAcl.AddAce(objAce2);
objSecDes.DiscretionaryAcl = objDAcl;
// Set permissions on the NTFS file system folder.
objADsSec.SetSecurityDescriptor(objSecDes,"FILE://" + vPath);
}
- Click the Form1.cs [Design] tab to switch
back to design mode.
- Double-click button1. Replace the
button1_Click event code with the following code.
private void button1_Click(object sender, System.EventArgs e)
{
try
{
// Set <Domain> to your domain name.
// Set <UserName> to the user account.
SetPermissions("C:\\Test", "<Domain>\\<UserName>");
MessageBox.Show("Full Access control granted.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} Note Replace <Domain> with the domain
name. Replace <UserName> with the name of the
user to whom you want to grant permissions. - On the Build menu, click Build
Solution.
Test the sample application
- Create a folder in the drive C root folder. Name the folder
Test.
- In Windows Explorer, right-click the Test
folder, and then click Properties.
- In the Test Properties dialog box, click
the Security tab.
- Select the domain account for which you are running this
test. If the account is not listed, click Add, and then add
the domain account to the list.
- Under Permissions, click to clear the
Full Control check box to restrict the permissions on the Test
folder for this user. Then, click OK.
- Run the NTFSPermission.exe application. By default,
Form1 is displayed.
- Click button1. You receive the following
message:
Full Access control
granted.
- Click OK to close the message
box.
- Close the form to quit the application.
- In Windows Explorer, open the C:\ folder.
- Right-click the Test folder, and then
click Properties.
- In the Test Properties dialog box, click
the Security tab.
- Select the domain account for which you are running this
test, and then verify the permissions on the Test folder.
The specified user now has Full Control permissions on the Test
folder.
For more information, click the following article numbers to
view the articles in the Microsoft Knowledge Base:
279682
(http://support.microsoft.com/kb/279682/
)
How to use ADsSecurity.dll to add an access control entry to an NTFS
folder
266461
(http://support.microsoft.com/kb/266461/
)
How to use ADSI to set automatic inheritance of file/folder permissions