【问题标题】:ASP.NET ascx control hosted in Sharepoint gets System.UnauthorizedAccessException when trying to save file托管在 Sharepoint 中的 ASP.NET ascx 控件在尝试保存文件时获取 System.UnauthorizedAccessException
【发布时间】:2012-06-26 08:23:13
【问题描述】:

我们有一个 ascx 自定义控件(不是 Web 部件)托管在一个特殊的 Sharepoint 页面中。此页面允许用户将文件上传到我们的服务器。不幸的是,权限问题阻止了 Sharepoint 将文件保存到网络位置。

归属于基于 Sharepoint 2007 的站点的应用程序池的网络帐户具有对该位置的“修改”和“读取”访问权限。

我们已使用应用程序池帐户使用的凭据登录到另一台计算机,并且可以在指定的网络位置创建目录和文件而不会出现任何问题。

Sharepoint 是否有可能尝试使用其他帐户来保存这些文件,而不是在 IIS7 的应用程序池中设置的那个?

我们得到的错误:

消息:对路径“\opal\gwl\pictures\L36”的访问被拒绝。

堆栈跟踪: 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.Directory.InternalCreateDirectory(String fullPath, String path, DirectorySecurity dirSecurity) 在 System.IO ECan.SharePoint.Web.Applications.MyECan_WaterMeterFormDatalogger.SavePhotos() 上的 .Directory.CreateDirectory(String path, DirectorySecurity directorySecurity)

异常类型: System.UnauthorizedAccessException

用户:系统帐户

ascx代码隐藏文件中SavePhotos函数的代码:

protected void SavePhotos()
{
    string wellNo = WellNo.Value;
    string epoWaterMeterID = EPO_WaterMeterID.Value;
    string dirRoot = ConfigurationManager.AppSettings["PhotoDir"];
    string map = wellNo.Substring(0, wellNo.IndexOf('/'));

    int photoSaveCount = 1;
    foreach (string filePath in Request.Files)
    {
        HttpPostedFile file = (HttpPostedFile)Request.Files[filePath];
        if (file.InputStream.Length > 0)
        {
            try
            {
                // Create dir if does not exist
                string dir = dirRoot + map;
                if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

                // Save file
                file.SaveAs(dir + @"\" + wellNo.Replace('/', '_') + "-" + epoWaterMeterID.ToString() + "-" + photoSaveCount.ToString() + ".jpg");

                photoSaveCount++;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
        }
    }
}

有人知道问题可能是什么吗?

【问题讨论】:

    标签: c# sharepoint permissions sharepoint-2007 ascx


    【解决方案1】:

    您是否尝试设置新创建的目录或文件夹的权限?您可以使用 System.Security.AccessControl 命名空间中的 DirectorySecurity 类,特别是该类的 SetAccessControl 方法。

    【讨论】:

      【解决方案2】:

      我认为您必须以提升的权限调用 SavePhotos。 以提升的权限运行代码将执行具有完全控制权限的指定方法,即使用户没有完全控制权限。

      查看链接:

      http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges(v=office.12).aspx

      请尝试以下代码:

      protected void Button1_Click(object sender, EventArgs e)
      {
         SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(SavePhotos);
         SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups);
      }
      

      【讨论】:

      • 我已经应用了代码更改,但我仍然遇到同样的问题。不过,感谢您的想法,将其隐藏起来以供将来参考!对测试框的进一步调查似乎指向 IIS 上的应用程序池使用的帐户,因为测试帐户没有问题。
      • 原来是 Sharepoint 保存文件的上下文。只是一开始没有抓住它,因为测试环境应该完全反映实时环境。在测试 Sharepoint 服务器上,文件是使用运行 IIS 应用程序池的同一帐户保存的,但在 live box 上,它使用的是 IUSR。
      猜你喜欢
      • 2019-04-10
      • 2011-05-28
      • 1970-01-01
      • 2015-02-13
      • 2014-03-23
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多