如何使用 C# 以编程方式删除文件夹权限上的孤立帐户
How to remove orphaned accounts on folder permission Programmatically whith c#
我有一个小程序可以从共享文件夹中删除帐户权限。但是在某些文件夹的安全选项卡上有这样的帐户 "S-1-5-21-2008445439-890656017-1691616715-1589748"。
我有权登录该服务器并手动删除,但由于下面的错误,我无法使用我的代码。我怎样才能删除这些帐户。谢谢
private void button2_Click(object sender, EventArgs e)
{
var security = Directory.GetAccessControl(txtBoxPath.Text);
var rules = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in rules)
{
if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
{
string name = rule.IdentityReference.Value;
RemoveFileSecurity(txtBoxPath.Text, name,
FileSystemRights.FullControl |
FileSystemRights.Modify |
FileSystemRights.Read |
FileSystemRights.ReadAndExecute |
FileSystemRights.ReadPermissions |
FileSystemRights.Synchronize |
FileSystemRights.ListDirectory |
FileSystemRights.ChangePermissions |
FileSystemRights.Delete,
AccessControlType.Allow);
MessageBox.Show("OK");
}
}
}
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
'System.Security.Principal.IdentityNotMappedException' 类型的未处理异常发生在 mscorlib.dll
附加信息:无法翻译部分或所有标识引用。
我检查了这段代码(如果重要的话,使用 .NET 4.0):IdentityReference 不会发生异常。
读取foreach循环中的条目是可以的,如果ACE(访问控制条目)包含无法解析的受托者(用户或组),它returns一个SID(S-1 -5-21-20084454....) 作为价值。这一点很好,框架代码在这里可以做的最好。
稍后您将帐户提供给
new FileSystemAccessRule(account, ...
此时出现异常,因为 account
将被视为帐户名称,并且将进行 SID 查找的名称。由于 "S-1-5..." 不是有效的帐户名,因此构造函数抛出。
但是:为什么要使用字符串作为 RemoveFileSecurity
方法的参数?
我稍微修改了一下代码:
foreach (FileSystemAccessRule rule in rules)
{
if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
{
RemoveFileSecurity(path, rule);
MessageBox.Show("OK");
}
}
public static void RemoveFileSecurity(string fileName, FileSystemAccessRule rule)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(rule);
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
希望我正确理解了您的问题。我假设您确实在文本框中输入了 SID,并希望删除带有 SID 的条目。
我有一个小程序可以从共享文件夹中删除帐户权限。但是在某些文件夹的安全选项卡上有这样的帐户 "S-1-5-21-2008445439-890656017-1691616715-1589748"。 我有权登录该服务器并手动删除,但由于下面的错误,我无法使用我的代码。我怎样才能删除这些帐户。谢谢
private void button2_Click(object sender, EventArgs e)
{
var security = Directory.GetAccessControl(txtBoxPath.Text);
var rules = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in rules)
{
if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
{
string name = rule.IdentityReference.Value;
RemoveFileSecurity(txtBoxPath.Text, name,
FileSystemRights.FullControl |
FileSystemRights.Modify |
FileSystemRights.Read |
FileSystemRights.ReadAndExecute |
FileSystemRights.ReadPermissions |
FileSystemRights.Synchronize |
FileSystemRights.ListDirectory |
FileSystemRights.ChangePermissions |
FileSystemRights.Delete,
AccessControlType.Allow);
MessageBox.Show("OK");
}
}
}
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
'System.Security.Principal.IdentityNotMappedException' 类型的未处理异常发生在 mscorlib.dll
附加信息:无法翻译部分或所有标识引用。
我检查了这段代码(如果重要的话,使用 .NET 4.0):IdentityReference 不会发生异常。
读取foreach循环中的条目是可以的,如果ACE(访问控制条目)包含无法解析的受托者(用户或组),它returns一个SID(S-1 -5-21-20084454....) 作为价值。这一点很好,框架代码在这里可以做的最好。
稍后您将帐户提供给
new FileSystemAccessRule(account, ...
此时出现异常,因为 account
将被视为帐户名称,并且将进行 SID 查找的名称。由于 "S-1-5..." 不是有效的帐户名,因此构造函数抛出。
但是:为什么要使用字符串作为 RemoveFileSecurity
方法的参数?
我稍微修改了一下代码:
foreach (FileSystemAccessRule rule in rules)
{
if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
{
RemoveFileSecurity(path, rule);
MessageBox.Show("OK");
}
}
public static void RemoveFileSecurity(string fileName, FileSystemAccessRule rule)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(rule);
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
希望我正确理解了您的问题。我假设您确实在文本框中输入了 SID,并希望删除带有 SID 的条目。