Access a network share from an asp.net app. Works in Visual Studio but not in IIS, why?

StackOverflow https://stackoverflow.com/questions/5341771

  •  27-10-2019
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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"/>
  ...

نصائح أخرى

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;

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.

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" />

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top