gpt4 book ai didi

c# - 如何在 .NET Core 3.1 中访问和审核文件的安全性

转载 作者:行者123 更新时间:2023-12-03 20:51:14 29 4
gpt4 key购买 nike

我现在正在努力使用 .NET Core 3.1 访问文件。我偶然发现了几个例子,但似乎它们都不起作用,或者我做错了什么。因此,任何建议或示例都将受到高度赞赏。
我使用的第一个示例如下:

var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
AccessFileControl.AddFileSecurity(this.LocalReport.ReportPath, everyone, FileSystemRights.WriteData, AccessControlType.Allow);
ChangeFontFamily(fontFamily);
AccessFileControl.RemoveFileSecurity(this.LocalReport.ReportPath, everyone, FileSystemRights.WriteData, AccessControlType.Deny);


// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, SecurityIdentifier indentifier,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
var security = new FileSecurity(fileName,
AccessControlSections.Owner |
AccessControlSections.Group |
AccessControlSections.Access);

security.ModifyAccessRule(AccessControlModification.Add, new FileSystemAccessRule(indentifier,
rights, controlType), out bool modified);
}
在上面的例子中,我有 该进程不拥有此操作所需的“SeSecurityPrivilege”特权。 将 AccessControlSection 更改为 All 时也会发生
var security = new FileSecurity(fileName,AccessControlSections.All);
然后在第二个例子中,我尝试整合稍微不同的方法
   var ac = new FileInfo(fileName).GetAccessControl();
// Get a FileSecurity object that represents the
// current security settings.

var security = new FileSecurity(fileName,
AccessControlSections.Owner |
AccessControlSections.Group |
AccessControlSections.Access);

security.ModifyAccessRule(AccessControlModification.Add, new FileSystemAccessRule(indentifier,
rights, controlType), out bool modified);

ac.AddAccessRule(new FileSystemAccessRule(indentifier,
rights, controlType));

ac.SetAccessRule(new FileSystemAccessRule(indentifier,
rights, controlType));

FileSystemAclExtensions.SetAccessControl(new DirectoryInfo(fileName), ds);
返回对路径“....”的访问被拒绝。
在第三个示例中,我尝试使用这种方法:
    public static void AddFileSecurity(string fileName, SecurityIdentifier indentifier,
FileSystemRights rights, AccessControlType controlType)
{

// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(fileName);

// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();

// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(indentifier,
rights, controlType));

// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
在第三个例子中,我有 试图执行未经授权的操作。
公平地说,我不知道我做错了什么,任何帮助表示赞赏
编辑 1
作为用户 jdweng 指出我只试过 AccessControlSections.Owner 但没有任何运气。
我也有同样的错误 拒绝访问路径“....” .
编辑 2
在最后一次尝试中,我沿着这条路尝试了一些东西,但又一次出现了 试图执行未经授权的操作。
试图:
public static void AddFileSecurity(string fileName, string directoryPath, SecurityIdentifier indentifier,
FileSystemRights rights, AccessControlType controlType)
{

var access = new FileInfo(fileName).GetAccessControl();

access.AddAccessRule(new FileSystemAccessRule(indentifier,
rights, controlType));


FileSystemAclExtensions.SetAccessControl(new FileInfo(fileName), access);
}

// Removes an ACL entry on the specified file for the specified account.
public static void RemoveFileSecurity(string fileName, SecurityIdentifier indentifier,
FileSystemRights rights, AccessControlType controlType)
{

var access = new FileInfo(fileName).GetAccessControl();

access.RemoveAccessRule(new FileSystemAccessRule(indentifier,
rights, controlType));

FileSystemAclExtensions.SetAccessControl(new FileInfo(fileName), access);

}
}

最佳答案

您可能会考虑以管理员身份运行您的应用程序。将这个 list 文件添加到您的项目中:runasadmin.manifest:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>

<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />

</dependentAssembly>
</dependency>

<v3:trustInfo xmlns:v3="urn:schemas-microsoft-com:asm.v3">
<v3:security>
<v3:requestedPrivileges>
<v3:requestedExecutionLevel level="highestAvailable" />
</v3:requestedPrivileges>
</v3:security>
</v3:trustInfo>
</assembly>
如果这不起作用或者您不想以管理员身份运行您的应用程序(或者如果您已经以管理员身份运行您的应用程序),请评论我..

关于c# - 如何在 .NET Core 3.1 中访问和审核文件的安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62655155/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com