【问题标题】:Why are permissions wonky when my IIS worker saves a file in ASP.NET?当我的 IIS 工作人员在 ASP.NET 中保存文件时,为什么权限会出现问题?
【发布时间】:2014-04-17 15:03:43
【问题描述】:

IIS 8、ASP.NET MVC 4、.NET 4.5

private static string SaveProfilePicFile(UserProfileViewModel model)
{
    var tempFilename = Path.GetTempFileName();

    model.ProfilePic.Profile.UploadedFile.SaveAs(tempFilename);

    var staticContentFilename = Helpers.GetStaticContentFilename(
        StaticContentType.Avatar, model.ProfilePic.Profile.UserId);

    var destinationFilename = Path.Combine(
        ConfigurationManager.AppSettings["StaticContentPath"],
        "profile",
        staticContentFilename);

    if (File.Exists(destinationFilename))
        File.Delete(destinationFilename);

    if (!HasJpegHeader(tempFilename)) // convert temp file into JPG
    {
        using (var image = new Bitmap(tempFilename))
            image.Save(destinationFilename, ImageFormat.Jpeg);

        File.Delete(tempFilename);
    }
    else
    {
        File.Move(tempFilename, destinationFilename);
    }

    return staticContentFilename;
}

我对代码审查不感兴趣,我知道事情可以做得更好。现在我遇到了一个不寻常的问题。 StaticContentPath 指向 c:\inetpub\wwwroot\static.domain.com,它由不同的应用程序池提供服务,该池被配置为禁用脚本并缓存更重的内容。如果我手动将文件放在静态内容文件夹中,它将正确提供。如果上面的代码(来自不同的应用程序池)在那里保存了一个文件,那么权限是非常不寻常的。我会附上截图。

“默认”文件是我手动粘贴的。它正确地从父文件夹继承了权限。哈希文件名是由上面的代码保存的,它没有正确继承权限。当我尝试访问该文件时,我从 IIS 收到一条非常基本的错误消息,其全部内容是“页面无法显示,因为发生了内部服务器错误”。没有样式,没有我习惯看到的 IIS 错误。如果我手动将读取权限添加到 IIS_IUSRS 帐户,一切都会按我的预期工作。

为什么会发生这种情况,我可以做些什么来缓解它,我的代码是否需要更新?

【问题讨论】:

  • 也发布高级对话框的屏幕截图。
  • 发布了其他截图。

标签: c# asp.net iis permissions ntfs


【解决方案1】:

我怀疑问题出在使用Path.GetTempFileName 后跟File.Move。当上传的文件保存到tempFilename 时,临时文件将获得分配给临时文件夹的任何权限。移动文件会按原样保留这些权限,而不是根据目标重新计算可继承的权限。

尝试使用File.Copy 后跟File.Delete,而不是File.Move

//File.Move(tempFilename, destinationFilename);
File.Copy(tempFilename, destinationFilename);
File.Delete(tempFilename);

【讨论】:

猜你喜欢
  • 2013-02-05
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2013-12-15
  • 2019-03-16
  • 2013-07-04
相关资源
最近更新 更多