Solved

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

Posted on 2008-10-22
1
8,834 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 27
Easy filter aspnet 2 27
Retrieve Active Directory Groups a User belongs to in VB.NET 3 20
Reactjs with .NET 3 37
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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