473,508 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting permissions on a folder using WMI

Hello.

I am writing some code that accepts a DFS Link and Username and grants that
User permissions to the physical directory that the DFS Link corresponds to.
I am using the System.Management namespace and WMI queries.

When I run the code below and check the Security tab of the folder , I find
that all entries have been cleared – which is ok and this is expected, main
problem is that the User that I have set up the Trustee and ACE object for,
has not been added. Interestingly (just before I set the permissions) I
retrieve the first ACE in the access list and then add it back into the
DACL[] property of the SecurityDescriptor object. In the code below, this
where I add ‘firstAce’ to the DACL property instead of ‘Ace’. When I check
the folder, the entry is added to the list.

I can probably assume that the code that actually sets the permissions does
work. What must be going wrong is either the way I have configured the
Trustee or the ACE object. The user to which we need to set permissions for
can be identified by the SID. I recover the SIDString using a WMI query,
passing in the Username and Domain, and then convert the resultant string
value into a byte array (SID needs to be in this format). I do this
conversion using the ASCIIEncoding class of System.Text, perhaps this is
where things are going wrong. Is there a more effective way of converting
from String to Byte Array?

Any thoughts on where there could be issues ?

Thanks,

Praveen.

Here is the code below:

//***TRUSTEE***

//Create Trustee management object (Win32_Trustee) setting the SID
(converted to byte array) and Username

ManagementObject Trustee = new ManagementClass(new
ManagementPath("Win32_Trustee"),null).CreateInstan ce();

Trustee["SID"] = bSID; //SID as a byte array
//***ACE***

//Create ACE management object (Win32_ACE) setting the AccessMask, AceFlags,
AceType and Trustee (to Trustee object)

ManagementObject Ace = new ManagementClass(new
ManagementPath("Win32_ACE"),null).CreateInstance() ;

Ace["AccessMask"] = "2032127";

Ace["AceFlags"] = "3";

Ace["AceType"] = 0;

Ace["Trustee"] = Trustee;

//***SecurityDescriptor***

//Retrieve the Security Descriptor passing in the path to the physical
directory

string dirClassPath1 = @"Win32_LogicalFileSecuritySetting='" + strDFSLink +
"'";

ManagementObject Win32LogicalFileSecuritySetting = new ManagementObject(new
ManagementPath(dirClassPath1),null);

ManagementBaseObject outParams1 =
Win32LogicalFileSecuritySetting.InvokeMethod("GetS ecurityDescriptor",null,
null);

ManagementBaseObject SecurityDescriptor1 = (ManagementBaseObject)
outParams1["Descriptor"];

//Get the first ACE in the existing DACL for this folder

ManagementBaseObject firstAce = ((ManagementBaseObject[])
SecurityDescriptor1["DACL"])[0];

//Set parameters for Security Descriptor

SecurityDescriptor1["ControlFlags"] = "4";

SecurityDescriptor1["DACL"] = new object[1]{Ace};

//ALTERNATIVELY SET TO THE FIRST ENTRY IN THE EXISTING DACL

//SecurityDescriptor1["DACL"] = new object[1]{firstAce};
//***Set Permissions

string dirClassPath2 = @"Win32_Directory='" + strDFSLink + "'";

ManagementObject Win32Directory = new ManagementObject(new
ManagementPath(dirClassPath2),null);

ManagementBaseObject inParams2 =
Win32Directory.GetMethodParameters("ChangeSecurity Permissions");

inParams2["Option"] = "4";

inParams2["SecurityDescriptor"] = SecurityDescriptor1;

ManagementBaseObject outParams2 =
Win32Directory.InvokeMethod("ChangeSecurityPermiss ions", inParams2, null);

Sep 24 '05 #1
0 2319

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2315
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
2
1772
by: Maximus | last post by:
Hi, Does anybody know how I can programatically set modify permissions on a folder for the aspnet user. Any help will be appreciated. Thanks.
0
7907
by: Johan | last post by:
Hi I'm using WMI to set and remove folderpermissions and it sems to work fine, sometimes. I start by having the folderpermissons manuly set to Everyone and Everone has full rights. When I'm...
1
1389
by: Jeremy Winchell | last post by:
I have an applications that imports new AD users. It creates a folder to store the user profile information, and a folder for their home directory. I would like to set the Permissions on these...
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
745
by: Praveen | last post by:
Hello. I am writing some code that accepts a DFS Link and Username and grants that User permissions to the physical directory that the DFS Link corresponds to. I am using the System.Management...
2
3723
by: steggun | last post by:
Hello, I'm writing a C# windows form application and need some guidance on a couple of issues. First, my application needs to change the permissions on a folder. The application creates a...
0
2092
by: =?Utf-8?B?TGlhbSBNYWM=?= | last post by:
Hi Folks, I have embeded WMI scripting within a Visual Basic application to create remote shares and set permissions, I'm now moving to vb.net environment and having trouble getting my scripting...
5
2155
by: daokfella | last post by:
I have a custom web.config section similar to the following: <CustomAuthSettings attr1="" attr2=""> <Locations RedirectUrl="Invalid.aspx"> <add Path="test.aspx" Roles="1,2,3" Permissions="4,5,6"...
0
7224
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
7120
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
7323
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,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
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...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...
0
415
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.