.NET Web 服务器上的文件系统多次访问权限
全部, 我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您遇到的问题是 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.
您可以在
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.检查应用程序池中配置的用户(通常是NETWORK SERVICE)是否有权删除它们。
Check if the user configured in the Application Pool (usually NETWORK SERVICE) has permission to delete them.