.NET Web 服务器上的文件系统多次访问权限

发布于 2024-12-17 04:22:15 字数 1272 浏览 0 评论 0原文

全部, 我的 Web 服务器的 Web 根目录中有一个 ~/temp 目录。比方说: C:\inetpub\myapp\temp

,我在页面的代码隐藏类中有一个方法,可以删除那里的所有文件。一切都运转良好。

protected CleanTemp() {
    // clean all the files in tmp dir
    Array.Foreach...
}

问题是,如果我在 Global.aspx 中创建一个计时器,尝试以相同的方式执行此类清理,系统会列出临时目录中的文件,但对于每个文件,它都会返回一个异常,表示用户没有正确的访问权限:

[Global.aspx]

void Application_Start(object sender, EventArgs e) {
    PhisicalTmpFolder = Server.MapPath(~/temp);

    // create new timer
    System.Timers.Timer timScheduledTask = new System.Timers.Timer();
    timScheduledTask.Interval = 20 * 60 * 1000.0;
    timScheduledTask.Enabled = true;
    timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
}


void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    // clean all the files in tmp dir
    Array.ForEach(
            Directory.GetFiles(PhisicalTmpFolder),
            delegate(string path) {
                try {
                    File.Delete(path); i++;
                } catch (Exception ex) {
                    if (_logger != null) _logger.Error(ex.Message, ex);
                }
            }
        );
}

我如何告诉计时器它必须以与应用程序相同的访问权限运行?

先感谢您, 詹皮耶罗

All,
I have a ~/temp dir on my web root in the web server. Let's say:
C:\inetpub\myapp\temp

and I have a method in the code behind class of a page that deletes all the files there. Everything works well.

protected CleanTemp() {
    // clean all the files in tmp dir
    Array.Foreach...
}

The problems is that if I create a timer in my Global.aspx that tries to execute such clean in the same way, the system lists the files in the temp dir but for each of them it returns an exception saying the user does not have the proper access rights:

[Global.aspx]

void Application_Start(object sender, EventArgs e) {
    PhisicalTmpFolder = Server.MapPath(~/temp);

    // create new timer
    System.Timers.Timer timScheduledTask = new System.Timers.Timer();
    timScheduledTask.Interval = 20 * 60 * 1000.0;
    timScheduledTask.Enabled = true;
    timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
}


void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    // clean all the files in tmp dir
    Array.ForEach(
            Directory.GetFiles(PhisicalTmpFolder),
            delegate(string path) {
                try {
                    File.Delete(path); i++;
                } catch (Exception ex) {
                    if (_logger != null) _logger.Error(ex.Message, ex);
                }
            }
        );
}

How can I tell to the timer that it must run with the same access right of the application?

Thank you in advance,
Gianpiero

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

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

发布评论

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

评论(3

说谎友 2024-12-24 04:22:15

我认为您遇到的问题是 ASP.NET 进程帐户没有适当的权限,并且您启用了用户模拟,因此当在页面请求期间启动操作时,它是在具有权限的请求用户帐户下完成的。

尝试授予 ASP.NET 进程帐户对该目录的权限。

I think the problem you have here is that the ASP.NET process account does not have the appropriate rights and you have user impersonation enabled so when the operation is initiated during a page request it is done so under the request user account which does have rights.

Try granting the ASP.NET process account rights to that directory.

∞琼窗梦回ˉ 2024-12-24 04:22:15

您可以在 timScheduledTask_Elapsed 方法中使用模拟来更改为具有必要提升权限的身份吗?

这里有一篇很棒的文章,描述了如何轻松地在 try / finally 块中创建模拟上下文。

Can you use impersonation inside your timScheduledTask_Elapsed method to change to an identity that has the necessary elevated permissions?

There is an excellent post here that describes how you can easily create an impersonation context in a try / finally block.

旧时模样 2024-12-24 04:22:15

检查应用程序池中配置的用户(通常是NETWORK SERVICE)是否有权删除它们。

Check if the user configured in the Application Pool (usually NETWORK SERVICE) has permission to delete them.

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