Solved

Setting read/write/modify rights to everyone in a folder in C#

Posted on 2008-10-22
1
8,809 Views
Last Modified: 2013-12-17
I'm copying a complete website into a specific location and I'm triying to give rights to everyone(LOCAL) to every folder in it because the user can download and make the server create files and folders, I'm using Win32_UserAccount and Win32_Account but for most users I'm getting the following error(Including aspnet which I think is the most important):

System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.
   at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
   at System.Security.Principal.NTAccount.Translate(Type targetType)
   at System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
   at System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
   at System.Security.AccessControl.FileSystemSecurity.AddAccessRule(FileSystemAccessRule rule)
   at


Check out my code below for more information.


SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain<>'"

                                     + System.Environment.UserDomainName.ToString() + "'");

                ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);

                foreach (ManagementObject mObject in mSearcher.Get())

                {

                        DirectorySecurity myDirectorySecurity = folder.GetAccessControl();

                        string User = System.Environment.UserDomainName + "\\" + mObject["Name"].ToString();

                                                myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.CreateFiles, AccessControlType.Allow));

                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Modify, AccessControlType.Allow));

                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Traverse, AccessControlType.Allow));

                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.ChangePermissions, AccessControlType.Allow));

                        //myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Modify, AccessControlType.Allow));

                        folder.SetAccessControl(myDirectorySecurity);

                                    }

Open in new window

0
Comment
Question by:CMS-Team
1 Comment
 
LVL 8

Accepted Solution

by:
the_crazed earned 500 total points
ID: 22785750
you've used your domain rather than the user's domain - your line 7 above should be
string User = mObject["Domain"].ToString() + "\\" + mObject["Name"].ToString();

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now