3

I have a web service using .net c# and I want to write to a text file on the server, but I cannot get this to work. I believe it's a permission problem.

Specifically, I think the problem is I am using System.IO.Directory.GetCurrentDirectory().

Is there a better alternative?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
user23149
  • 553
  • 2
  • 6
  • 13
  • PLease provide a cutdown example of your code. – Mitch Wheat Oct 24 '08 at 14:13
  • Need more information than that, I think. From your description, the problem could literally be anything. – TheSmurf Oct 24 '08 at 14:13
  • I dunno, while the problem may be more subtle, I figured it was exactly what StingyJack answered below - the account IIS is running as doesn't have write permission to the directory where the file goes and if the file is to be written to a network share, the IIS account must be a domain account. – CMPalmer Oct 24 '08 at 14:28

5 Answers5

9

Try granting the ASP.NET user (or whatever account IIS is running as) permission to write to the folder you are trying to write to.

If this is a network share, try to run IIS as a domain user that can write to the share.

Remember the principle of granting minimal permission (dont use Admin level access).

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • I've seen this problem several times and this is almost always the correct approach to solving it - check the permissions on the IIS user and if it's a network share you're writing to, it has to be a domain user. – CMPalmer Oct 24 '08 at 14:29
3

If you don't specify a destination folder I assume your web service wants to write to "C:\Windows\System32\" or something of that kind. That's why a UnauthorizedAccessException will be thrown on the server. In order to write to the "home"-directory of the web service you have to find out where that is first.

The following works for me but you also find other suggestions here.

Add the reference System.Web, and get the current directory by calling:

strFileDestination = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + strFileName;

Remember to give your service write permission in IIS.

Community
  • 1
  • 1
simaglei
  • 1,118
  • 1
  • 14
  • 14
2

If you're running on Windows 2003 and haven't turned on ASP.NET impersonation, and are running the app in the DefaultAppPool or an application pool that is configured to run under the identity of "Network Service", then you'll need to give the "Network Service" account write permission to the destination folder. If you're running the site in an app pool that is using an identity other than "Network Service" then that account may require write permissions to the destination folder.

If you're running windows 2000 then the '<MACHINENAME>\ASPNET' account will need write permissions to the destination folder.

If you've got impersonation turned on then you'll need to give the site's anonymous user account write permissions to the destination folder instead.

To check if impersonation is turned on, open (assuming ASP.NET 2.0) then check your machine.config file (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) to see if you have the following setting:

<identity impersonate="true"/>

This may also be overridden in your application's web.config.

Additionally if you're running in a partial trust environment then you'll likely only be able to write to the website's application folder because the default FileIOPermission is usually set to $AppDir$, i.e. you can't modify files anywhere else, even with the correct NTFS permissions.

If you're writing to a network share then StingyJack has the answer for you, but the partial trust environment considerations still apply.

But check your NTFS perms first, that's probably the best bet.

Hope this helps Kev

Community
  • 1
  • 1
Kev
  • 118,037
  • 53
  • 300
  • 385
2

Stuart, instead of using System.IO.Directory.GetCurrentDirectory(), you may want to use Server.MapPath. You can give Server.MapPath the directory relative to the location of your web service where you want to save the file, otherwise you probably need to pass a full file path "C:\Files\file.txt". As far as the permissions issue, I am usually able to resolve this by adding write access to the folder I'm writing the file to for IIS_WPG and ASPNET users (These usernames may be different on your server).

Austin
  • 4,638
  • 7
  • 41
  • 60
0

You should be able to write files from web services. This is most likely a permissions or trust issue. If you are in a limited trust (i.e., Medium trust) ensure that you are writing to a patch in or below your web root. If you are already doing that, or are in a full trust environment check to make sure that the directory has permissions for the IIS worker process to write to it.

Jason Whitehorn
  • 13,585
  • 9
  • 54
  • 68