[英]Access a network share from an asp.net app. Works in Visual Studio but not in IIS, why?
I have a network share in the intranet that allows write access to everyone (guest) 我在Intranet中有一个网络共享,该共享允许对所有人(来宾)的写访问
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. 因此,我在c#中编写了一个程序(mojoportal模块,无论如何都是这样),该程序在该路径中写入了一些文件,并且在Visual Studio中效果很好。
Now, using the same program in IIS 7.5, does not work, i get this exception: 现在,在IIS 7.5中使用相同的程序不起作用,我得到此异常:
System.Net.WebException: An exception occurred during a WebClient request. System.Net.WebException:WebClient请求期间发生异常。 ---> System.IO.IOException - Logon failure: unknown user name or bad password. ---> System.IO.IOException-登录失败:用户名未知或密码错误。
Well... since the access on the smb share is granted to everybody, i don't understand why it does not work on IIS... 嗯...由于smb共享的访问权限已授予所有人,我不明白为什么它在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. 默认情况下,未授予您的IIS服务访问文件共享的权限。 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: 您可以尝试将应用程序池标识设置为Local System来运行站点,或者手动控制从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
. 看起来访问SMB共享不是问题,访问urltodownload
存储的网站也不是问题。 Like Richard said, you may want to use the default credentials ie wc.UseDefaultCredentials = true;
就像Richard所说的,您可能要使用默认凭据,即wc.UseDefaultCredentials = true;
or some specific set of credentials or set up the web site at urltodownload
to accept anonymous connections. 或某些特定的凭据集,或在urltodownload
上设置网站以接受匿名连接。
Refer to what can cause WebException
for the DownloadFile
method @ MSDN . 有关DownloadFile
方法@ MSDN,请参阅导致WebException
的原因。
You need some kind of credentials to use a share from asp.net. 您需要某种凭据才能使用来自asp.net的共享。 You can setup an impersonation on the web.config or set wc.Credentials using network credentials. 您可以在web.config上设置模拟或使用网络凭据设置wc.Credentials。 In the web.config you would set the follwoing under the system.web section: 在web.config中,您可以在system.web部分下设置以下内容:
<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. 对于我的Visual Studio应用程序(WinForms),这是创建供应用程序使用的强名称键(SNK)的简单操作。
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. 也许在Visual Studio中创建SNK可以设置我上面其他作者所指的这些凭据。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.