0

共有フォルダーからアカウントのアクセス許可を削除する小さなプログラムがあります。しかし、一部のフォルダのセキュリティ タブには、「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 で発生しました

追加情報: 一部またはすべての ID 参照を翻訳できませんでした。

4

1 に答える 1

0

私はこのコードをチェックしました(問題がある場合は.NET 4.0で):IdentityReferenceによって例外は発生しません。

foreach ループ内のエントリの読み取りは問題ありません。ACE (アクセス制御エントリ) に解決できないトラスティ (ユーザーまたはグループ) が含まれている場合、SID (S-1-5-21-20084454...) が返されます。 .) 値として。現時点ではこれで問題なく、フレームワーク コードがここで実行できる最善の方法です。

後でアカウントを渡す

new FileSystemAccessRule(account, ...

この時点で、例外が発生します。これaccountは、アカウント NAME として認識され、名前から 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 を含むエントリを削除したいと思います。

于 2015-02-20T13:00:05.627 に答える