1

I have a network share in the intranet that allows write access to everyone (guest)

So, i wrote a program (mojoportal module, anyway it's the same, i think) in c# that writes some files in that path, and in visual studio works great.

Now, using the same program in IIS 7.5, does not work, i get this exception:

System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException - Logon failure: unknown user name or bad password.

Well... since the access on the smb share is granted to everybody, i don't understand why it does not work on IIS...

The code is this:

WebClient wc = new WebClient();
wc.DownloadFile(urltodownload, smbshare);

What it can be? Thanks

Magnetic_dud
  • 1,506
  • 2
  • 22
  • 36
  • That's crazy, it only works if i run the app as Administrator, I cannot do that... – Magnetic_dud Mar 17 '11 at 20:48
  • 1
    I reproduced your issue. See my answer: Your IIS service not granted to access file share by default. You could try run your site with app pool identity set to Local System. Or you had setup special permissions for shared folder ? – mastak Mar 17 '11 at 21:16
  • As Local system (SYSTEM) it does not work on win 2008 r2. Works only as Administrator and if i load the user profile... – Magnetic_dud Mar 17 '11 at 21:21
  • What do you mean "it does not work on win 2008 r2" ? Does it throw exception: System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path '...' is denied. at System.IO... ? It works great on my Win 2008 r2 with app pool identity set to: Local System or Network Service.. if I use Local Service then it not work.. or some restrictions set for network policy. – mastak Mar 18 '11 at 09:26
  • Maybe it's my code: before downloading checks the share to see if the file it has already been downloaded (cached) or not – Magnetic_dud Mar 18 '11 at 11:33

5 Answers5

5

Your IIS service not granted to access file share by default. You could try run your site with app pool identity set to Local System, or manually control the identity under which code is executed from web.config:

<system.web>
  <identity impersonate="true" userName="user with correct rights or admin" password="password"/>
  ...
mastak
  • 1,397
  • 1
  • 14
  • 28
1

Have you tried setting the credentials?

I believe that the everyone group still requires a valid credential of some sort, without setting the credentials you are anonymous...

wc.Credentials = CredentialCache.DefaultCredentials;
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
0

It looks like its not an issue with accessing the SMB share, its an issue accessing the website stored in urltodownload. Like Richard said, you may want to use the default credentials i.e. wc.UseDefaultCredentials = true; or some specific set of credentials or set up the web site at urltodownload to accept anonymous connections.

Refer to what can cause WebException for the DownloadFile method @ MSDN.

Adam Price
  • 10,027
  • 1
  • 20
  • 16
  • Oh, i was wrong to copy the exception, it's System.IO.IOException - Logon failure: unknown user name or bad password. (because it's in italian, so i translated it in english) – Magnetic_dud Mar 17 '11 at 16:16
0

You need some kind of credentials to use a share from asp.net. You can setup an impersonation on the web.config or set wc.Credentials using network credentials. In the web.config you would set the follwoing under the system.web section:

<identity impersonate="true" userName="username_here" password="password_here" />
Alex Mendez
  • 5,120
  • 1
  • 25
  • 23
0

For my Visual Studio application (WinForms), it was a simple act of creating a strong name key (SNK) for the application to use.

I'm not sure why.

Perhaps creating a SNK in Visual Studio has a way of setting these Credentials that the other authors above me are referring to.

Hope this helps.