从 Sharepoint 应用程序页面访问远程文件

发布于 2024-12-02 12:08:53 字数 1127 浏览 0 评论 0原文

在我的 Sharepoint 应用程序页面中,我尝试从网络共享文件夹复制文件。我的代码如下所示。

    try
    {
        File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true);
        LblMessage.Text = "File copied.";
    }
    catch (Exception ex)
    {
        LblMessage.Text = ex.ToString() + " - " + ex.Message;
    }

如果我在 ASP.NET 网站中测试相同的代码,它运行良好。但我收到错误如下 SP 应用程序页面..

System.UnauthorizedAccessException: Access to the path '\\MShare\Public\Test.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) at TestApp.PullingFile.ButGet_Click(Object sender, EventArgs e) - Access to the path '\\MShare\Public\Test.txt' is denied.

我尝试通过以下 帖子..不起作用。 。 我也尝试在 web.config 中使用 进行更改。

In my Sharepoint Application Page, I'm trying to copy a file from a Network Shared Folder.. And my code's like below..

    try
    {
        File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true);
        LblMessage.Text = "File copied.";
    }
    catch (Exception ex)
    {
        LblMessage.Text = ex.ToString() + " - " + ex.Message;
    }

It's working well if I test the same code in ASP.NET Website.. But I'm getting error as follow with SP Application Page..

System.UnauthorizedAccessException: Access to the path '\\MShare\Public\Test.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) at TestApp.PullingFile.ButGet_Click(Object sender, EventArgs e) - Access to the path '\\MShare\Public\Test.txt' is denied.

I've tried implementing impersonation by following this post.. Not working..
And I tried by changing at web.config with <trust level="WSS_Medium" originUrl="" /> also..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-12-09 12:08:53

默认情况下,SharePoint 使用模拟。这意味着您的代码在当前用户的凭据下运行。该用户没有(也不应该)访问服务器的文件系统。

您可以做的是恢复到系统帐户凭据< /a> 为了访问文件系统:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true);
});

必须记住的事情:

  1. 系统帐户(应用程序池帐户)不一定能够访问文件系统(最低权限场景)。 (SO问题
  2. 系统帐户(应用程序池帐户)不一定有权访问网络共享。
  3. 访问您的应用程序页面的任何用户都可以执行您的文件复制代码。您必须自己关心授权。

最后但并非最不重要的
为什么必须将文件复制到服务器的文件系统?您是否需要在服务器上实际使用它,或者这只是暂时的(正如人们可以通过路径猜到的那样)。

By default SharePoint uses impersonating. Meaning your code runs under the credentials of the current user. This user has not (and should not have) access to the server's file system.

What you can do is revert to the system accounts credentials in order to access the file system:

SPSecurity.RunWithElevatedPrivileges(() =>
{
    File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true);
});

What you have to keep in mind:

  1. The system account (application pool account) has not necessarily access to the file system (least privileges scenario). (SO question)
  2. The system account (application pool account) has not necessarily access to the network share.
  3. Any user accessing your application page can execute your file copy code. You have to care about authorization yourself.

Last but not least:
Why do you have to copy the file to the server's file system after all? Do you need it physically on the server or is this just temporary (as one could guess by the path).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文