1

我正在尝试编写代码以从我的工作站上的应用程序以编程方式在服务器上创建目录(并执行其他文件操作)——使用 Directory.CreateDirectory 这很容易,而且我知道该怎么做。但是,问题是我试图在我的用户 ID 无权这样做的服务器上执行此操作。我确实有一个 A/D 用户 ID 可以使用它,但我不知道如何在我的应用程序中使用它来做我需要做的事情(模拟不是它所谓的,但是......)。

这是我想做的事情:

System.Security.AccessControl.DirectorySecurity ds = new System.Security.AccessControl.DirectorySecurity();

// <-- something magic happens here -->

Directory.CreateDirectory(@"\\ofmsws42\c$\New_Directory", ds);

“魔法”发生的地方是什么?还是我在叫错树?我想说我的服务器凭据最终位于我正在创建的 DirectorySecurity 对象中的某个位置,但 DirectorySecurity 的所有属性似乎都不起作用。

4

1 回答 1

3

You need to impersonate with the account that have permissions in "magic code".

WindowsIdentity.Impersonate have sample (referenced from SO: How do you do Impersonation in .NET?)

Here are most important chunks of code (LogonUser is PInvoke from advapi32.dll):

// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
      LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
      out safeTokenHandle);

using (WindowsImpersonationContext impersonatedUser = 
   WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
{
...
}
于 2012-07-11T00:15:26.673 回答