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

£可爱£侵袭症+ 提交于 2019-12-24 07:46:20

问题


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


回答1:


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



回答2:


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;



回答3:


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.




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/5341771/access-a-network-share-from-an-asp-net-app-works-in-visual-studio-but-not-in-ii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!