Nant:更改文件权限

发布于 2024-07-07 15:51:17 字数 497 浏览 6 评论 0原文

我有一个 ASP.NET 应用程序。 基本上,交付过程是这样的:

  • Nant 构建应用程序,并在开发人员的计算机上创建一个 zip 文件,其中包含应用程序文件,不含 SVN 文件夹和无用文件。 该文件与 Nant 脚本一起提供。
  • zip 和 nant 文件被复制到客户端计算机,
  • Nant 脚本将当前网站文件替换为 zip 文件中包含的文件。

我的问题是,通过此过程,当我尝试打开该网站时出现未经授权的访问错误。 这些文件似乎需要为用户“IIS_WPG”设置权限。

我无权更改 IIS 配置,因此我必须手动更改每个文件的权限。 每次替换文件时,权限都会被删除,我需要再次设置它们。

所以我有两个问题:

  • 我可以使用 Nant 更改文件权限吗? 怎么做 ?
  • 有可能避免这个问题吗? (开发者的计算机上没有该用户)

I have an ASP.NET application.
Basically the delivery process is this one :

  • Nant builds the application and creates a zip file on the developer's computer with the application files without SVN folders and useless files. This file is delivered with a Nant script.
  • The zip and nant files are copied to the client's computer
  • the Nant script replaces the current website files with the file contained in the zip file.

My problem is that with this process I have an Unauthorized access error when I try to open the website.
It seems that the files need to have a permission set for the user "IIS_WPG".

I don't have the power to change IIS configuration so I have to manually change the permissions of each file. And each time I replace the files the permissions are removed and I need to set them again.

So I have two questions :

  • Can I change files permissions with Nant ? How to do it ?
  • Is it possible to avoid this problem ? (developers don't have this user on their computers)

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

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

发布评论

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

评论(4

温折酒 2024-07-14 15:51:17

@杰夫·弗里茨
哎哟...
您的建议是正确的解决方案,但参数很......危险:)。

在开发计算机上,我以管理员身份登录,并使用 cmd 尝试了您的建议。

  • 它替换了所有设置的权限,以便仅设置命令中定义的权限(因此,在命令之后,即使使用我的管理员用户,访问文件也会导致“访问被拒绝”)
  • 它应用于 C:\WINDOWS\ 目录,当我从 wwwroot 文件夹调用命令时。 :)

因此,经过一些测试,正确的命令是:

cacls [full folder path] /T /E /G IIS_WPG:F
  • /T :应用于指定的文件夹和子文件夹
  • /E :编辑 ACL,而不是替换它:)

@Jeff Fritz
Ouch...
Your suggestion is the right solution but the parameters are... dangerous :).

On dev computers I'm logged as administrator and I tried your suggestion with cmd.

  • It replaces all the permissions set in order to set only the ones defined in the command (so, after the command, accessing files resulted in a "Access denied" even with my admin user)
  • It applied on the C:\WINDOWS\ directory, while I called the command from the wwwroot folder. :)

So, after some tests, the right command is :

cacls [full folder path] /T /E /G IIS_WPG:F
  • /T : applies on specified folder and subfolders
  • /E : edits the ACL instead of replacing it :)
尹雨沫 2024-07-14 15:51:17

您需要在Windows中运行CACLS程序来授予文件和文件夹权限。 在 Nant 中,您可以使用 EXEC 任务来完成此操作。

尝试使用如下标签块:

<exec program="cacls">
    <arg value="*" />
    <arg value="/G IIS_WPG:F" />
</exec>

You need to run the CACLS program in windows to grant permissions to files and folders. From Nant, you can do this with the EXEC task.

Try a tag block like:

<exec program="cacls">
    <arg value="*" />
    <arg value="/G IIS_WPG:F" />
</exec>
蓝颜夕 2024-07-14 15:51:17

我们最终用一些相当简单的代码为此编写了自己的任务:

[TaskName("addusertodir")]
public class AddUserToDirectorySecurity : Task
{
    [TaskAttribute("dir", Required=true)]
    public string DirPath { get; set; }

    [TaskAttribute("user", Required=true)]
    public string UserName { get; set; }

    protected override void ExecuteTask()
    {
        FileSystemAccessRule theRule1 = new FileSystemAccessRule(UserName, FileSystemRights.ListDirectory, AccessControlType.Allow);
        FileSystemAccessRule theRule2 = new FileSystemAccessRule(UserName, FileSystemRights.ReadAndExecute, AccessControlType.Allow);
        FileSystemAccessRule theRule3 = new FileSystemAccessRule(UserName, FileSystemRights.Read, AccessControlType.Allow);

        DirectorySecurity theDirSecurity = new DirectorySecurity();
        theDirSecurity.AddAccessRule(theRule1);
        theDirSecurity.AddAccessRule(theRule2);
        theDirSecurity.AddAccessRule(theRule3);
        Directory.SetAccessControl(DirPath, theDirSecurity);
    }
}

然后您可以编写一个加载自定义任务并执行的 nant 脚本:

<loadtasks>
    <fileset>
        <include name="MyTask.dll"/>
    </fileset>
</loadtasks>

<addusertodir dir="MyDir" user="IIS_WPG"/>

显然,这可以根据您的某些规则进行修改,或者您甚至可以在任务中对其进行参数化如果你愿意的话。 我们更喜欢这个而不是使用 exec 任务,因为它让我们对正在应用的权限有更多的控制。

We ended up writing our own task for this with some fairly straight forward code:

[TaskName("addusertodir")]
public class AddUserToDirectorySecurity : Task
{
    [TaskAttribute("dir", Required=true)]
    public string DirPath { get; set; }

    [TaskAttribute("user", Required=true)]
    public string UserName { get; set; }

    protected override void ExecuteTask()
    {
        FileSystemAccessRule theRule1 = new FileSystemAccessRule(UserName, FileSystemRights.ListDirectory, AccessControlType.Allow);
        FileSystemAccessRule theRule2 = new FileSystemAccessRule(UserName, FileSystemRights.ReadAndExecute, AccessControlType.Allow);
        FileSystemAccessRule theRule3 = new FileSystemAccessRule(UserName, FileSystemRights.Read, AccessControlType.Allow);

        DirectorySecurity theDirSecurity = new DirectorySecurity();
        theDirSecurity.AddAccessRule(theRule1);
        theDirSecurity.AddAccessRule(theRule2);
        theDirSecurity.AddAccessRule(theRule3);
        Directory.SetAccessControl(DirPath, theDirSecurity);
    }
}

Then you can write a nant script that loads the custom task and executes:

<loadtasks>
    <fileset>
        <include name="MyTask.dll"/>
    </fileset>
</loadtasks>

<addusertodir dir="MyDir" user="IIS_WPG"/>

Obviously, this could be modified for your certain rules or you could even parameterize this in the task if you so wish. We preferred this over the using the exec task as it have us a bit more control over permissions that were being applied.

绾颜 2024-07-14 15:51:17

CACLS 现已弃用。 这是使用 ICACLS 的替代版本。

假设我们有以下内容:

  • 我们安装的根文件夹是“c:\inetpub\wwwroot”,它存储在 NANT 变量 ${paths.myprogram.inetpub}
  • 我们想要的文件夹修改称为“uploads”,它存储在 ${upload.foldername}
  • 我们要授予访问权限的用户是“IIS_UPLOAD_USER”,存储在 ${iis.upload.user}
  • 我们要授予的权限级别是“M”,代表“修改”权限,存储在 ${iis.user.permissionlevel}

有了这些假设,我们的任务是这样的:

<exec program="icacls">
    <arg value="${path::combine(paths.myprogram.inetpub, upload.foldername)}" />
    <arg value="/grant" />
    <arg value="${iis.upload.user}:${iis.user.permissionlevel}" />
</exec>

希望这样有帮助!

CACLS is now deprecated. Here's a version that uses ICACLS, the replacement.

Let's say we have the following:

  • The root folder of our installation is "c:\inetpub\wwwroot", and it's stored in the NANT variable ${paths.myprogram.inetpub}
  • The folder we want to modify is called "uploads", and it's stored in ${upload.foldername}
  • The user we want to grant access to is "IIS_UPLOAD_USER", stored in ${iis.upload.user}
  • The permission level we want to grant is "M", for "modify" permissions, stored in ${iis.user.permissionlevel}

With these assumptions, our task is this:

<exec program="icacls">
    <arg value="${path::combine(paths.myprogram.inetpub, upload.foldername)}" />
    <arg value="/grant" />
    <arg value="${iis.upload.user}:${iis.user.permissionlevel}" />
</exec>

Hope this helps!

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