简体   繁体   English

将文件复制到IIS中的Web应用程序文件夹会导致文件权限问题

[英]Copying files to web application folder in IIS causes file permission issues

We have been having issues migrating to a new IIS server as many of the documents accessible through our web apps cannot be edited. 由于无法编辑通过Web应用程序访问的许多文档,因此在迁移到新的IIS服务器时遇到了问题。 This includes things such as XML files we use for settings. 这包括诸如我们用于设置的XML文件之类的东西。

When we try and overwrite existing files (that have been copied from the old IIS server) with new versions, for instance a new settings.xml file, we get permission denied. 当我们尝试使用新版本覆盖现有文件(从旧IIS服务器复制的文件)时,例如新的settings.xml文件,我们将获得拒绝权限。 We have a number of things to give the app pool permission to overwrite files but we have not been successful. 我们有很多事情可以赋予应用程序池覆盖文件的权限,但还没有成功。 This includes giving the NETWORK SERVICE user account full control over the file as well as the folder. 这包括授予NETWORK SERVICE用户帐户对文件以及文件夹的完全控制权。

This issue, however, goes away if we delete these files and regenerate them from scratch. 但是,如果我们删除这些文件并从头开始重新生成它们,此问题将消失。 For instance the settings.xml file can be deleted manually, then the app can regenerate one with default values. 例如,可以手动删除settings.xml文件,然后该应用可以使用默认值重新生成一个文件。 This works fine. 这很好。

Basically the issue is that if we copy files ourselves into the folder, the web app throws an 基本上,问题在于,如果我们将文件自己复制到文件夹中,则网络应用会抛出一个

"Access to the path [..FILENAME..] denied" “拒绝访问路径[..FILENAME ..]”

however we remove that file and allow the web app to generate the file on it's own, it then has full access to the file and can overwrite/write to it without an issue. 但是,我们会删除该文件,并允许网络应用自行生成文件,然后它就可以完全访问该文件,并且可以覆盖/写入文件而不会出现问题。

While we can obviously go through this process file by file, we are looking for a more sustainable solution so that we do not have keep deleting/regenerating files in the future. 虽然显然可以逐个文件地处理该过程,但我们正在寻找一种更具可持续性的解决方案,以便将来不再需要删除/重新生成文件。

Here are a couple of things to try. 这里有一些尝试的方法。 One edits the file security to remove any "deny" access permissions and to give your app full rights, and the other removes any "read only" settings on a file and sets the attributes to "normal" (I've had to use this one in the past): 一个编辑文件安全性以删除所有“拒绝”访问权限并为您的应用授予完全权限,另一个编辑者删除文件上的所有“只读”设置并将属性设置为“普通”(我必须使用此设置)过去的一个):

protected void Page_Load(object sender, EventArgs e)
{
    string path = Server.MapPath("theFileLocation");
    RemoveFileSecurity(path, @"App Pool Identity", FileSystemRights.FullControl, AccessControlType.Deny);
    AddFileSecurity(path, @"App Pool Identity", FileSystemRights.FullControl, AccessControlType.Allow);

    FileAttributes a = File.GetAttributes(path);
    a = RemoveAttribute(a, FileAttributes.ReadOnly);
    File.SetAttributes(path, FileAttributes.Normal);
}

private FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
    return attributes & ~attributesToRemove;
}


private void AddFileSecurity(string fileName, string account, FileSystemRights rights, AccessControlType controlType)
{
    FileSecurity fSecurity = File.GetAccessControl(fileName);
    fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
    File.SetAccessControl(fileName, fSecurity);
}

private void RemoveFileSecurity(string fileName, string account, FileSystemRights rights, AccessControlType controlType)
{
    FileSecurity fSecurity = File.GetAccessControl(fileName);
    fSecurity.RemoveAccessRule(new FileSystemAccessRule(account, rights, controlType));
    File.SetAccessControl(fileName, fSecurity);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 授予IIS Web应用程序访问网络驱动器上文件的权限 - Give IIS web application permission to access files on network drive 无法从Web应用程序访问IIS 6虚拟文件夹中的文件 - Unable To Access File In IIS 6 Virtual Folder From Web Application 在编译时将文件复制到应用程序文件夹中 - Copying files into the application folder at compile time 使用 C# 控制台应用程序为 IIS 应用程序池设置文件夹权限 - Set folder permission for a IIS application pool with C# Console application ASP.NET Web应用程序无法找到我的XML文件,说它不在IIS Express文件夹中 - ASP.NET Web application cant find my XML file, sayis its not in IIS Express folder IIS Shadowcopy无法复制xml文件 - IIS Shadowcopy not copying xml file 将projectname.dll文件从我的Visual Studio项目复制到IIS bin文件夹中是正确的 - Copying the projectname.dll file from my visual studio project to my IIS bin folder is this correct 从iis上托管的Web应用程序读取/写入共享驱动器中的文件 - Read/write files in shared drive from web application hosted on iis MSBuild 没有将文件复制到发布文件夹? - MSBuild not copying files to the publish folder? JIRA Web请求导致问题 - JIRA web request causes issues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM