473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DirectorySecurity IdentityNotMappedException

Hello,

I'm trying to add a DirectorySecurity ACL entry to an existing
directory (based on some code I found in the msdn).

However, this code only works on local machines because I can't specify
a server to resolve the identity, so when I try on a remote machine it
just can't find the user.

Any ideas how the following code can be applied to a remote server?

// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);

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

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

// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
catch (IdentityNotMappedException e)
{
// Exception caught when username is invalid.
}

Cheers everyone!

Dec 30 '05 #1
7 9172


"Nick" <ni**@renhome.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello,

I'm trying to add a DirectorySecurity ACL entry to an existing
directory (based on some code I found in the msdn).

However, this code only works on local machines because I can't specify
a server to resolve the identity, so when I try on a remote machine it
just can't find the user.

Any ideas how the following code can be applied to a remote server?

// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);

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

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

// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
catch (IdentityNotMappedException e)
{
// Exception caught when username is invalid.
}

Cheers everyone!

Not sure what you mean with a remote server here, but I guess you mean that
FileName is a UNC path right?
Well, you can't do that, the code must run on the server where the 'object'
is located, and the account must be a local account or a domain account.

Willy.
Dec 30 '05 #2
Hi Willy,

Yes you're exactly right, and I expected as much. The account is a
local account on a remote machine.

Is there an alternative way of setting security permissions on UNC
paths without having to call commands like xcacl?

Nick

Dec 30 '05 #3

"Nick" <ni**@renhome.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi Willy,

Yes you're exactly right, and I expected as much. The account is a
local account on a remote machine.

Is there an alternative way of setting security permissions on UNC
paths without having to call commands like xcacl?

Nick


Yes, there are:
- Run your code on the remote server, or if that's no option,
- use WMI (System.Management) to set/change ACL permissions.
But I would prefer xcacls.

Willy.

Dec 30 '05 #4
Well, WMI was my first option, but after trawling the msdn library for
a while, I just couldn't find any classes that seemed to do the job.
Possibly because I've only really had experience with IIS WMI classes
so far.

Any idea what classes I should be looking for?

Dec 30 '05 #5

"Nick" <ni**@renhome.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Well, WMI was my first option, but after trawling the msdn library for
a while, I just couldn't find any classes that seemed to do the job.
Possibly because I've only really had experience with IIS WMI classes
so far.

Any idea what classes I should be looking for?


Well using WMI is a non trivial task, there are a number of WMI classes you
need to know and you need a solid grasp of file object security in windows.

Following is basically what you need to do to add an ACE to a DACL on a
remote system.
- Connect to the remote server using WMI, beware that you need appropriate
access privs to the remote server and WMI.
- Create an instance of Win32_LogicalFileSecuritySetting passing the
filesystem object as Path
- call it's GetSecurityDescriptor, using
InvokeMethod("GetSecurityDescriptor,...)
- get the DACL from the SD
- create/initialize a Win32_Trustee instance with the name of the account
you like to add to the DACL
- create/initialize a new Win32_ACE class and set the above trustee as
Trustee property, set the other properties (AccessMask, AceFlags and
AceType)
- add the ACE to the DACL
- rewrite the DACL by calling InvokeMethod("SetSecurityDescriptor", ..)

Willy.

Dec 30 '05 #6
Great help wally, that looks like all I need to know. Thanks for the
class names!

Nick

Dec 30 '05 #7
Finally got round to writing the method to apply security ACE's, and
this is what I've come up with so far. However, the userAccount and
newAce objects seem to be blank. And so it's just adding a blank ACE to
the first element in the DACL.

ManagementScope scope = new ManagementScope(@"\\" + ServerName +
@"\root\cimv2");

ManagementPath path = new ManagementPath();
path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path="
+ "'" + fileName + "'";

ManagementObject fileSecurity = new ManagementObject(
scope, path, null);

ManagementBaseObject outParams =
(ManagementBaseObject)fileSecurity.InvokeMethod(
"GetSecurityDescriptor", null, null);

// Get security descriptor and DACL for specified file.
ManagementBaseObject descriptor =
(ManagementBaseObject)outParams.Properties["Descriptor"].Value;
ManagementBaseObject[] dacl =
(ManagementBaseObject[])descriptor.Properties["Dacl"].Value;

// Get the user account to be trustee.
ManagementObject userAccount = new ManagementClass(scope,
new ManagementPath("Win32_Trustee"), null).CreateInstance();
userAccount.Properties["Name"].Value = account;

// Create a new ACE for the descriptor.
ManagementObject newAce = new ManagementClass(scope,
new ManagementPath("Win32_ACE"), null).CreateInstance();
newAce.Properties["Trustee"].Value = userAccount;

// Low level ace flags.
int FILE_READ_DATA = 0x0;
int FILE_WRITE_DATA = 0x1;
int FILE_APPEND_DATA = 0x4;
int DELETE = 0x10000;

// Translate FileSystemRights to flags.
switch (accessRights)
{
case FileSystemRights.Read:
newAce.Properties["AccessMask"].Value = FILE_READ_DATA;
break;

case FileSystemRights.Modify:
newAce.Properties["AccessMask"].Value = FILE_READ_DATA
| FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE;
break;
}

// ACL will be inherited.
newAce.Properties["AceFlags"].Value = 0x10;

// Allow access to resource.
newAce.Properties["AceType"].Value = 0;

// Add ACE to DACL and set to descriptor.
dacl.SetValue(newAce, 0);
descriptor.Properties["Dacl"].Value = dacl;

// User SetSecurityDescriptor to apply the descriptor.
ManagementBaseObject inParams =
fileSecurity.GetMethodParameters("SetSecurityDescr iptor");
inParams["Descriptor"] = descriptor;
fileSecurity.InvokeMethod("SetSecurityDescriptor", inParams, null);

Jan 10 '06 #8

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

Similar topics

1
by: Jason Steeves | last post by:
I am using the following code to get the properties of a folder and everything is working ok but I need to find the "Current owner of this item" for the specified folder. Dim fileprops As...
0
by: Marcus Dano | last post by:
Hi, I want to read all ACLs from a single folder in my NTFS partition. I'am using following code: Dim dir As DirectoryInfo = New DirectoryInfo("C:\demo") Dim sec As DirectorySecurity =...
0
by: Matt | last post by:
In trying to view the ACL of a directory some (not all) domain groups are not having their identities translated. If I use FileSecurity to list out the groups with access to a particular file in...
4
by: DavidMGorman | last post by:
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...
1
by: Aek | last post by:
What is the best way to recursively change the permissions of the directory we are installing into? Is there a nice way to do this in C# ..NET? We are using an MSI installer and will need to add...
0
by: Edhy Rijo [Progytech] | last post by:
Hi All, I am very new to VB.NET and creating small project to that will copy all files from a DVD to a folder in the Hard Drive. While doing the copy around 30% I got the following error: ...
10
by: DragonLord | last post by:
I am creating a folder from my application and attempting to write from the filestream to the folder, however when I do I get an access denied exception. I have tried adding security rights to the...
4
by: kanepart2 | last post by:
Hey , I need to make a function in C# that gets the permissions on a folder and its sub folders. I wanted to list the users and the permissions they have been granted on folders. I dont know if it...
0
by: theindescribablehunk | last post by:
Hi, I previously used DirectoryInfo objects to access the DirectorySecurity object so i could enumerate the ACL for specific directories. Unfortunately the DirectoryInfo object doesn't support...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.