1

in SharePoint 2010, I created a DLL that I deploy to the GAC. From this assembly, I try to save a file then access the file I just saved in a temporary location, such as C:\temp. Everytime I try to do so, I get:

Access to the path 'C:\temp' is denied.

So I go to this folder on the server, and add Everyone to the security settings and give Everyone Full Control as a test; same result.

Any ideas what could be causing this? My DLL is correctly referenced in the Web.Config:

<SafeControl Assembly="My_SharePoint_Namespace", Version=1.0.0.0, Culture=neutral, PublicKeyToken="abcdefghijklmnop" Namespace="My_SharePoint_Namespace" TypeName="*" Safe="True" SafeAgainstScript="True" />

Any ideas why I would be getting this error?

using (FileStream stream = new FileStream(file_info.Directory.FullName, FileMode.Create, FileAccess.ReadWrite))
{
    stream.Write(fileBytes, 0, fileBytes.Length);
}
1
  • I am very sorry for the long wait, but I failed to share one piece of information; the SP instance is running on Windows 7. We moved it to an actual server, but I haven't been able to test this. I will re-visit this issue, so please don't think I didn't read your answers. Thanks for your patience. Commented Jul 13, 2012 at 17:44

2 Answers 2

1

If your site uses Anonymous access, this could be the issue as the Anonyous ID is not included in 'Everyone'. You might need to wrap your code in the RunWithElevatedPrivileges() command so that it runs under the ID of the application pool

SPSecurity.RunWithElevatedPrivileges(delegate() {
  using (SPSite site = new SPSite("siteUrlOrID")) {
    using (SPWeb web = site.OpenWeb("webUrlOrID")) {
      ... other code to access the file bytes ... 
      using (FileStream stream = new FileStream(file_info.Directory.FullName, FileMode.Create, FileAccess.ReadWrite))
      {
        stream.Write(fileBytes, 0, fileBytes.Length);
      }
    }   
  }
});
2
  • Hi, very good tip, I did not know that Anonymous was not part of Everyone. However, Anonymous is disabled in this case. I did try your solution anyway, but sadly it did not work. Commented Jun 1, 2012 at 21:13
  • Did you use the SPSecurity.RunWithElevatedPrivileges() call as shown above - with ALL of your code inside the block including the retrieval of the SPSite object? If you are using SPContext.Current.Site then it is the context of the user, not the appPool, regardless of where you use it so it is important that you fetch a new SPSite object within the ElevatedPrivs block.
    – Dave Wise
    Commented Jun 1, 2012 at 22:00
1

By default, FileStream needs ReadWrite access that’s why System.UnauthorizedAccessException is thrown because on a production machine, User account under which asp.net worker process runs or a windows service or for that matter any process will not have the write access to a file by default.

Please make sure you get the reference to any web is constructed INSIDE the SPSecurity.RunWithElevatedPrivileges- delegate. I'm quite sure that this will solve your problem!

You can look in Debug-mode which user is set within the web when you are inside the delegate (Property: SPWeb.CurrentUser).

Also make sure that, which user account is your ApplicationPool identity? Have you granted permissions on your folder to this account?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.