【发布时间】:2012-12-10 13:02:49
【问题描述】:
我正在使用下面的代码来更改文件的所有者。此代码在用 C# 4.0 版编写的 Windows 服务中运行。它在本地服务器上运行,运行 Windows Server 2008 R2 Standard,Service Pack 1。
我需要将通过 FTP 接收的文件的所有者更改为域帐户。我可以登录该框并使用资源管理器手动执行此操作,但是当我尝试通过代码运行此操作时,我得到一个 InvalidOperation 异常。我可以将所有者更改为本地系统帐户,但不能更改为网络帐户。对此的任何帮助将不胜感激。
我正在使用一些奇怪的 Microsoft Dynamics AX 代码来处理 EDI 文件。该过程要求文件的所有者是有效的 DAX 用户,在本例中为域用户。我们有供应商通过 FTP 向我们发送 EDI 数据。我们的 DAX 应用程序每 10 分钟检查一次 FTP 目录并处理文件。该过程当前失败,因为所有者无效。因此,我编写了一个服务来在文件到达时更改文件的所有者。但是,下面的代码失败,代码示例下方显示异常。
var ediFileOwner = new NTAccount("MyDomain", _ediEndpointUserAccount);
var fileSecurity = File.GetAccessControl(fileName);
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
fileSecurity.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule(ediFileOwner, FileSystemRights.TakeOwnership, AccessControlType.Allow));
fileSecurity.SetOwner(ediFileOwner); //Change our owner from to our desired User
File.SetAccessControl(fileName, fileSecurity);
这里是完整的例外:
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 exceptionContext)
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)
更新
如果我将运行服务的帐户更改为我尝试更改为所有者的帐户,我会遇到不同的异常。
意外异常:System.UnauthorizedAccessException:尝试 执行未经授权的操作。在 System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType 类型, 字符串名称、SafeHandle 句柄、SecurityInfos 安全信息、 SecurityIdentifier 所有者、SecurityIdentifier 组、GenericAcl sacl、 GenericAcl dacl)
【问题讨论】:
-
需要更多信息。为什么不直接复制文件并删除原始文件。这将创建一个具有正确权限的新文件夹。否则,如果您希望使用您的代码,则需要有关已上传文件的更多信息。完整的例外也很有帮助。
-
我已经添加了例外和我正在尝试做的事情的完整描述,或者至少我为什么要这样做。
-
将您刚刚发布的评论放在您的问题中。