部分或全部标识引用无法翻译为 c#

  • 本文关键字:翻译 引用 全部 标识 c#
  • 更新时间 :
  • 英文 :


我正在尝试向用户授予访问该文件夹的权限,但是当我尝试运行该程序时,错误显示:Some or all identity references could not be translated

这是我正在使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Management;
using System.Management.Instrumentation;
namespace FolderLock
{
    public partial class Lock : Form
    {
        public Lock()
        {
            InitializeComponent();
            SetAccess();
        }
        private void Lock_Load(object sender, EventArgs e)
        {
        }
        public void SetAccess()
        {
            DirectoryInfo myDirectoryInfo = new DirectoryInfo("C:/Users/Trov/Desktop/Test");
            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
            string User = System.Environment.UserDomainName + "\" + "92111092";
            myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
        }
    }
}

我已经找到了一种方法,而不是尝试允许或拒绝特定用户对该文件夹的访问,我只是创建一个众所周知的经过身份验证的用户来拒绝或允许它访问该文件夹。

这是代码:

public void SetAccess()
        {
            DirectoryInfo myDirectoryInfo = new DirectoryInfo(@"C:/Users/Trov/Desktop/Test");
            var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); 
            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
            myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Read, AccessControlType.Deny));
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
            this.Hide();
            this.Close();
        }

谢谢

当我使用应用组安全性但使用用户的代码时,我遇到了此错误。这是我所做的:

'code called:
'THIS CODE IS CALLED IN THE SAME DOMAIN AS THE USER I NEED TO ADD
ApplyUserSecurity({user1,user2}.ToList(), "C:temp", FileSystemeRights.Modify)

'Method called
Private Sub ApplyUserSecurity(identities As List(Of String), path As String, accessType As FileSystemRights)
     Dim dirInfo As New DirectoryInfo(path)
     Dim dirSec As DirectorySecurity = dirInfo.GetAccessControl(AccessControlSections.All)
     Dim acls = dirSec.GetAccessRules(True, True, GetType(Security.Principal.NTAccount))
    Dim identityFound As Boolean = False
    Dim applyChanges As Boolean = False
    For Each identity As String In identities
        identityFound= False
        For Each acl As FileSystemAccessRule In acls
            If identity = acl.IdentityReference.Value Then
                identityFound = True
                Exit For
            End If
        Next
    
        If Not identityFound Then
            dirSec.AddAccessRule(New FileSystemAccessRule(identity, accessType , AccessControlType.Allow))
            dirSec.AddAccessRule(New FileSystemAccessRule(identity, accessType , InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
            applyChanges = True
        End If
    Next
    'Apply changes only once for performance reason
    If applyChanges Then
        dirInfo.SetAccessControl(dirSec)
    End If
End Sub

以及我如何为组应用安全性(修改)。

If Directory.Exists("C:temp") Then
     folderInfo = New IO.DirectoryInfo("C:temp")
     folderAcl = folderInfo.GetAccessControl()
     folderAcl.AddAccessRule(New FileSystemAccessRule("domaingroupName", FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
     folderInfo.SetAccessControl(folderAcl)
end If

最新更新