Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-22
1
Medium Priority
?
8,863 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
the_crazed earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

664 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