我可以更改通过 IIS(使用 ASP.NET)保存的文件的所有者吗

发布于 2024-07-13 01:14:28 字数 117 浏览 4 评论 0原文

有没有办法在 Windows Server 上使用 IIS 更改已保存文件的所有者。 越容易越好。 在文件已保存到光盘后,必须在保存或更改文件所有者期间完成此操作并不重要。 ASP.NET 中的一个示例非常受欢迎。

Is there a way to change owner of saved file using IIS on Windows Server. The easier the better. It doesn't matter either this will have to be done during saving or changing file owner after file is already saved to disc. An example in ASP.NET is highly apriciated.

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

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

发布评论

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

评论(3

唠甜嗑 2024-07-20 01:14:28

从理论上讲,它应该相当简单。 您应该能够执行类似的操作来更改现有文件的所有权:

string domain = "domain";
string user = "username";

FileInfo info = new FileInfo(@"c:\test.txt");

FileSecurity security = info.GetAccessControl();

System.Security.Principal.NTAccount newOwner =
    new System.Security.Principal.NTAccount(domain, user);

security.AddAccessRule(
        new FileSystemAccessRule(newOwner, FileSystemRights.FullControl,
            AccessControlType.Allow));
security.SetAccessRuleProtection(true, false);
security.SetOwner(newOwner);

info.SetAccessControl(security);

但实际上,由于 Windows 施加的限制,这实际上不起作用。 Windows 不允许您将文件的所有者更改为当前用户或管理员组以外的任何人。

当它到达最后一行时,您将收到异常“安全标识符不允许成为该对象的所有者”。

谷歌搜索表明也许可以解决这个问题,但是当我过去尝试时,我未能让解决方法发挥作用。 我很想知道是否有人成功地解决了这个问题。

In theory it should be fairly straight forward. You should be able to do something like this to change the ownership of an existing file:

string domain = "domain";
string user = "username";

FileInfo info = new FileInfo(@"c:\test.txt");

FileSecurity security = info.GetAccessControl();

System.Security.Principal.NTAccount newOwner =
    new System.Security.Principal.NTAccount(domain, user);

security.AddAccessRule(
        new FileSystemAccessRule(newOwner, FileSystemRights.FullControl,
            AccessControlType.Allow));
security.SetAccessRuleProtection(true, false);
security.SetOwner(newOwner);

info.SetAccessControl(security);

In practice however this doesn't actually work because of a limitation that Windows imposes. Windows won't allow you to the change the owner of the file to anything other than the current user or the administrators group.

When it hits the last line you will get the exception "The security identifier is not allowed to be the owner of this object".

Googling suggests that it may be possible to work round this problem, but I have failed to get the work arounds to work when I have tried in the past. I'd be very interested to hear if anyone had successfully achieved the work around.

梦里兽 2024-07-20 01:14:28

如果用户具有恢复文件和目录权限,则可以将所有权分配给其他用户。 默认情况下此功能处于禁用状态,因此您需要在尝试设置所有者之前启用它。 .Net 没有对此的内置支持,因此您需要 PInvoke AdjustTokenPrivileges 并使用其他非托管函数来获取对此的输入。

我已经写了其使用的详细描述 在我的博客上

A user can assign ownership to other users if they have the Restore Files and Directories privilege. This is disabled by default so you need to enable it before trying to set the owner. .Net doesn't have built in support for this so you'll need to PInvoke AdjustTokenPrivileges and use other unmanaged functions to get the inputs to this.

I've written a detailed description of its use on my blog

太阳公公是暖光 2024-07-20 01:14:28

虽然这不是一个定制的示例,但我相信您的答案在于
System.Security.AccessControl 命名空间。

看一下 FileSecurity 类,它可以让您识别规则和权限。 FileSecurity 类由 File.Create() 等方法使用。

While this is not a customized example, I believe your answer lies in the
System.Security.AccessControl namespace.

Take a look at the FileSecurity Class which lets you identify rules and permissions. The FileSecurity class is used by methods such as File.Create().

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