使用 DirectoryEntry 创建文件夹
我正在编写一个 ASP.NET (C#) 应用程序来为我的域创建用户。它还必须在单独的文件服务器上创建文件夹和共享。到目前为止,我已经能够使用
- System.IO.Directory.CreateDirectory 创建文件夹、
- 使用(“WinNT://fileserver/lanmanserver”)DirectoryEntry 创建共享来完成我的任务。
不幸的是,我的 ASP.NET 应用程序必须以模拟方式运行才能创建文件夹。我不喜欢那样。我想知道是否有一种方法可以使用 DirectoryEntry 对象在文件服务器上创建文件夹,因为我可以将所需的凭据传递给其构造函数。或者,有没有办法将凭据传递给 Directory.CreateDirectory?
提前致谢。 这是当前的代码,以防万一
strPath = "\\myServer\D$\newDir";
Directory.CreateDirectory(strPath);
using (DirectoryEntry deFS = new DirectoryEntry("WinNT://myServer/lanmanserver"))
{
using (DirectoryEntry deSH = deFS.Children.Add("newDir$", "fileshare"))
{
deSH.Properties["path"].Value = "D:\\newDir";
deSH.Properties["description"].Value = "My Stackoverflow sample share";
deSH.CommitChanges();
}
}
I am writing an ASP.NET (C#) application to create users for my domain. It also has to create folders and shares on a separate file server. I have so far been able to accomplish my task using
- System.IO.Directory.CreateDirectory to create the folders,
- a ("WinNT://fileserver/lanmanserver") DirectoryEntry to create the shares.
Unfortunately, my ASP.NET application has to run with impersonation on to create the folder. I don't like that. I would like to know if there is a way to create a folder on the file server using a DirectoryEntry object since i can pass the needed credentials to its constructor. Or, alternatively, is there a way to pass credentials to Directory.CreateDirectory?
Thanks in advance.
Here is the current code, just in case
strPath = "\\myServer\D$\newDir";
Directory.CreateDirectory(strPath);
using (DirectoryEntry deFS = new DirectoryEntry("WinNT://myServer/lanmanserver"))
{
using (DirectoryEntry deSH = deFS.Children.Add("newDir$", "fileshare"))
{
deSH.Properties["path"].Value = "D:\\newDir";
deSH.Properties["description"].Value = "My Stackoverflow sample share";
deSH.CommitChanges();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不应该为此目的使用 DirectoryObject,它不是为此类访问而设计的。但您可以使用以下技巧来使模仿变得更容易。创建一个模拟类,它将实现 IDisposable,如下所示:
然后你就可以这样做:
I don't believe you should be using DirectoryObject for that purpose, it wasn't made for such an access. But here's a trick you could be using to make impersonation easier. Create an impersonator class, which would implement IDisposable, something like this:
then you would be able to do this:
据我所知,您有两个选择:模拟有权在远程共享上创建目录的用户,或将权限授予运行 asp.net 服务的默认用户。
这有什么问题吗?您正在访问网络上的非默认资源,默认权限不允许您这样做。这与尝试在网络共享上写入的普通用户帐户非常相似。
As far as I know you have two options: impersonate a user that has permissions to create the directory on the remote share or give the permissions to the default user that runs asp.net services.
What is wrong with that? You are accessing a non-default resource on your network and the default privileges dont allow you to do that. It's pretty much like a regular user account trying to write on a network share.
DirectoryEntry 类有一个构造函数,它将用户名和密码作为输入。你试过这个吗?
请参阅 Microsoft 文档
The DirectoryEntry class has a constructor which take username and password as input. Have you tried this?
See documentation at Microsoft