使用 DirectoryEntry 创建文件夹

发布于 2024-08-07 09:24:27 字数 837 浏览 3 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(3

新雨望断虹 2024-08-14 09:24:27

我认为您不应该为此目的使用 DirectoryObject,它不是为此类访问而设计的。但您可以使用以下技巧来使模仿变得更容易。创建一个模拟类,它将实现 IDisposable,如下所示:

public class Impersonator : IDisposable
{
    public Impersonator(userid, password) 
    {
        ... LogonUserEx();
        ... DuplicateToken();
        ... Impersonate();
    }
    public void Dispose()
    {
        ... RevertToSelf();
    }
}

然后你就可以这样做:

using(new Impersonator("myaccount", "password"))
{
     ... do stuff that requires impersonation
}

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:

public class Impersonator : IDisposable
{
    public Impersonator(userid, password) 
    {
        ... LogonUserEx();
        ... DuplicateToken();
        ... Impersonate();
    }
    public void Dispose()
    {
        ... RevertToSelf();
    }
}

then you would be able to do this:

using(new Impersonator("myaccount", "password"))
{
     ... do stuff that requires impersonation
}
往昔成烟 2024-08-14 09:24:27

据我所知,您有两个选择:模拟有权在远程共享上创建目录的用户,或将权限授予运行 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.

仙气飘飘 2024-08-14 09:24:27

DirectoryEntry 类有一个构造函数,它将用户名和密码作为输入。你试过这个吗?

请参阅 Microsoft 文档

The DirectoryEntry class has a constructor which take username and password as input. Have you tried this?

See documentation at Microsoft

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