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

被刻印的时光 ゝ 提交于 2019-12-31 04:34:07

问题


I'm having troubles figuring out the SetOwner() method. In my case, I've created an user in Active Directory by code, then, I create a folder for the user.

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. 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);

回答1:


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#

Bye.



来源:https://stackoverflow.com/questions/1435543/c-sharp-how-to-use-directorysecurity-setowner-im-having-troubles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!