检查.NET中的目录和文件写入权限在我的.NET 2.0应用程序中,我需要检查是否有足够的权限来创建和写入目录的文件。为此,我有以下函数尝试创建一个文件并向其写入一个字节,然后删除自己以测试权限是否存在。我认为检查的最佳方法是实际尝试并执行此操作,捕获发生的任何异常。虽然我对一般的异常捕获并不是特别高兴,所以有更好的或者更可接受的方式吗?private const string TEMP_FILE = "\\tempFile.tmp";/// <summary>/// Checks the ability to create and write to a file in the supplied directory./// </summary>/// <param name="directory">String representing the directory path to check.</param>/// <returns>True if successful; otherwise false.</returns>private static bool CheckDirectoryAccess(string directory){
bool success = false;
string fullPath = directory + TEMP_FILE;
if (Directory.Exists(directory))
{
try
{
using (FileStream fs = new FileStream(fullPath, FileMode.CreateNew,
FileAccess.Write))
{
fs.WriteByte(0xff);
}
if (File.Exists(fullPath))
{
File.Delete(fullPath);
success = true;
}
}
catch (Exception)
{
success = false;
}
}
3 回答
冉冉说
TA贡献1877条经验 获得超1个赞
您应该做的是计算运行代码的用户标识的有效权限。例如,以上示例中没有一个正确地考虑了组成员身份。
我很确定Keith Brown在他的Windows安全Windows开发人员指南的维基版本(此时是离线版)中有一些代码可以执行此操作。在他的Programming Windows Security书籍中也对此进行了详细讨论。
计算有效权限不适合胆小的人,而您的代码尝试创建文件并捕获抛出的安全异常可能是阻力最小的路径。
繁星coding
TA贡献1797条经验 获得超4个赞
Deny优先于Allow。本地规则优先于继承的规则。我已经看到了很多解决方案(包括这里显示的一些答案),但它们都没有考虑规则是否是继承的。因此,我建议采用以下方法来考虑规则继承(整齐地包装到类中):
public class CurrentUserSecurity{
WindowsIdentity _currentUser;
WindowsPrincipal _currentPrincipal;
public CurrentUserSecurity()
{
_currentUser = WindowsIdentity.GetCurrent();
_currentPrincipal = new WindowsPrincipal(_currentUser);
}
public bool HasAccess(DirectoryInfo directory, FileSystemRights right)
{
// Get the collection of authorization rules that apply to the directory.
AuthorizationRuleCollection acl = directory.GetAccessControl()
.GetAccessRules(true, true, typeof(SecurityIdentifier));
return HasFileOrDirectoryAccess(right, acl);
}
public bool HasAccess(FileInfo file, FileSystemRights right)
{
// Get the collection of authorization rules that apply to the file.
AuthorizationRuleCollection acl = file.GetAccessControl()
.GetAccessRules(true, true, typeof(SecurityIdentifier));
return HasFileOrDirectoryAccess(right, acl);
}
private bool HasFileOrDirectoryAccess(FileSystemRights right,
AuthorizationRuleCollection acl)
{
bool allow = false;
bool inheritedAllow = false;
bool inheritedDeny = false;
for (int i = 0; i < acl.Count; i++) {
var currentRule = (FileSystemAccessRule)acl[i];
// If the current rule applies to the current user.
if (_currentUser.User.Equals(currentRule.IdentityReference) ||
_currentPrincipal.IsInRole(
(SecurityIdentifier)currentRule.IdentityReference)) {
if (currentRule.AccessControlType.Equals(AccessControlType.Deny)) {
if ((currentRule.FileSystemRights & right) == right) {
if (currentRule.IsInherited) {
inheritedDeny = true;
} else { // Non inherited "deny" takes overall precedence.
return false;
}
}
} else if (currentRule.AccessControlType
.Equals(AccessControlType.Allow)) {
if ((currentRule.FileSystemRights & right) == right) {
if (currentRule.IsInherited) {
inheritedAllow = true;
} else {
allow = true;
}
}
}
}
}
if (allow) { // Non inherited "allow" takes precedence over inherited rules.
return true;
}
return inheritedAllow && !inheritedDeny;
}}但是,我的经验是,这并不总是适用于远程计算机,因为您无法始终查询那里的文件访问权限。在这种情况下的解决方案是尝试; 甚至可能只是尝试创建一个临时文件,如果您需要在使用“真实”文件之前知道访问权限。
- 3 回答
- 0 关注
- 576 浏览
添加回答
举报
0/150
提交
取消
