简体   繁体   English

C#-如何使用DirectorySecurity.SetOwner()? 我有麻烦

[英]C# - How to use DirectorySecurity.SetOwner() ? I'm having troubles

I'm having troubles figuring out the SetOwner() method. 我在弄清楚SetOwner()方法时遇到了麻烦。 In my case, I've created an user in Active Directory by code, then, I create a folder for the user. 就我而言,我已经通过代码在Active Directory中创建了一个用户,然后为该用户创建了一个文件夹。

It all works fine, but I cannot set the newly created user as the owner of the folder. 一切正常,但是我无法将新创建的用户设置为文件夹的所有者。 I'm not a C# guru, so I'm having troubles to understand the DirectorySecurity.SetOwner() method. 我不是C#专家,所以在理解DirectorySecurity.SetOwner()方法时遇到了麻烦。 Can anyone please help me out? 有人可以帮我吗?

Here is the code that creates the folder, sets the rights like i want it, but I also need to set the user as the owner. 这是创建文件夹,设置所需权限的代码,但我还需要将用户设置为所有者。

string pathIntern = @"\\11fil01\brukar\" + user.UserName;

System.IO.DirectoryInfo diIntern = new System.IO.DirectoryInfo(pathIntern);

diIntern.Create();

DirectorySecurity dsecIntern = diIntern.GetAccessControl();

FileSystemAccessRule rule = new FileSystemAccessRule(user.UserName, FileSystemRights.FullControl, InheritanceFlags.None | nheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);

dsecIntern.SetAccessRule(rule);
diIntern.SetAccessControl(dsecIntern);
//dsecIntern.SetOwner(heeeeelp);

Try this 尝试这个

string pathIntern = @"\\11fil01\brukar\" + user.UserName;               
DirectoryInfo diIntern       = new DirectoryInfo(pathIntern);
DirectorySecurity dsecIntern = diIntern.GetAccessControl();
IdentityReference newUser    = new NTAccount(domain + @”\” + username);
dsecIntern.SetOwner(newUser);
FileSystemAccessRule permissions = new FileSystemAccessRule(newUser,FileSystemRights.FullControl, AccessControlType.Allow);
dsecIntern.AddAccessRule(permissions);
diIntern.SetAccessControl(dsecIntern);

You can see this link too Create, Read, Update Active Directory Users with C# 您也可以看到此链接,使用C#创建,读取,更新Active Directory用户

Bye. 再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM