无法将文件所有者设置为 NT SERVICE\TrustedInstaller

发布于 2024-11-27 11:37:17 字数 1262 浏览 0 评论 0原文

使用资源管理器,我可以将我创建的文件的所有者设置为“NT SERVICE\TrustedInstaller”,并且它确实得到了设置。我已经用 DIR /Q 验证了。

但是,当我在 Windows 7 SP1 X64 上使用以下 C# 代码时:

FileInfo info = new FileInfo("TrustedFile.txt");
FileSecurity security = info.GetAccessControl();
NTAccount Sid = new NTAccount("NT SERVICE\\TrustedInstaller");
security.SetOwner ( Sid );
info.SetAccessControl( security );

以管理员权限运行时,出现以下异常事件。

**System.InvalidOperationException: The security identifier is not allowed to be the owner of this object.**
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object excepti
onContext)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
   at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
   at System.IO.File.SetAccessControl(String path, FileSecurity fileSecurity)
   at System.IO.FileInfo.SetAccessControl(FileSecurity fileSecurity)
   at SetFileOwner.Program.Main(String[] args)

知道我做错了什么吗?

Using explorer I can set the owner of a file I have created to "NT SERVICE\TrustedInstaller" and it does get set. I have verified with DIR /Q.

But when I use the following C# code on Windows 7 SP1 X64 :

FileInfo info = new FileInfo("TrustedFile.txt");
FileSecurity security = info.GetAccessControl();
NTAccount Sid = new NTAccount("NT SERVICE\\TrustedInstaller");
security.SetOwner ( Sid );
info.SetAccessControl( security );

I get the following exception, event when running with Administrator privileges.

**System.InvalidOperationException: The security identifier is not allowed to be the owner of this object.**
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object excepti
onContext)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
   at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
   at System.IO.File.SetAccessControl(String path, FileSecurity fileSecurity)
   at System.IO.FileInfo.SetAccessControl(FileSecurity fileSecurity)
   at SetFileOwner.Program.Main(String[] args)

Any idea what I am doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-04 11:37:17

您必须模拟一个有权在 ASP.NET 中执行此操作的 Windows 用户。

模拟通常用于 asp.net 中的数据库登录。

You have to impersonate a windows user that will have the rights to do so inside asp.net.

Impersonation is usually done for for Database login inside asp.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文