简体   繁体   English

使用C#Web API在NTFS服务器上创建文件夹

[英]Create a folder on NTFS server using C# web api

I am implementing an application that will create a folder on the NTFS File Server. 我正在实现一个将在NTFS文件服务器上创建文件夹的应用程序。 For that, I have set up the distributed environment. 为此,我建立了分布式环境。 Meaning, I have machine on which I have hosted my C# Web API and I have NTFS file server on which I have to create folders. 意思是,我有一台托管C#Web API的计算机,有一台必须创建文件夹的NTFS文件服务器。

I want my API to create a folder on NTFS machine. 我希望我的API在NTFS计算机上创建一个文件夹。 How can I do that? 我怎样才能做到这一点? Do I need to share NTFS Drive/Folder to create a subfolder? 我需要共享NTFS驱动器/文件夹来创建子文件夹吗? If so, then with whom I need to share a NTFS folder ( either server user or IIS_USRS )? 如果是这样,那么我需要与谁共享一个NTFS文件夹( 服务器用户或IIS_USRS )?

Is there any other way to create a folder without sharing a drive/folder on NTFS folder. 是否有其他方法可以创建文件夹而无需在NTFS文件夹上共享驱动器/文件夹。

When I have done this in the past I have done it through a temporary impersonation. 过去,当我这样做时,我是通过临时模仿来完成的。 For example... 例如...

using (Impersonator impersonate = new Impersonator(@"UserName", "Password", "Domain")) 
{
     //Create directory or copy files across the network
}

I believe I lifted my impersonator code from A small C# Class for impersonating a User 我相信我从A C#小类中删除了模拟人代码, 以模拟用户

Case 1: 情况1:

if web server and file server are in the same domain, you can consider to use a domain user, and created a shared folder on NTFS server and grant full access for the domain user accessing it. 如果Web服务器和文件服务器位于同一域中,则可以考虑使用域用户,并在NTFS服务器上创建了共享文件夹,并为访问它的域用户授予完全访问权限。 (depends on your requirements). (取决于您的要求)。

On web server, for the web application pool, set the domain user as the identity/credential to run the pool. 在Web服务器上,对于Web应用程序池,将域用户设置为运行该池的身份/凭据。 so you can easily use IIS management tool to update it if password gets changed. 因此,如果密码更改,您可以轻松地使用IIS管理工具对其进行更新。 for the authentication, you can use whatever you want based on requirements, but remember while you call the code to create folders on NTFS server, you need to use app pool user. 对于身份验证,您可以根据需要使用所需的任何内容,但是请记住,在调用代码以在NTFS服务器上创建文件夹时,需要使用应用程序池用户。 (for example, if you turn on impersonation on a different user in authentication, in your code, you need to do impersonation using the pool user.) (例如,如果在身份验证中对其他用户启用模拟,则在代码中,您需要使用池用户进行模拟。)

Case 2, web server and file server are not in the same domain, I usually will set up a ftp server on the file server to allow specific users to access it (creating folders and upload files....). 情况2,Web服务器和文件服务器不在同一域中,我通常将在文件服务器上设置ftp服务器,以允许特定用户访问它(创建文件夹和上传文件...。)。 Otherwise when you may need your IT administrator to make File server domain trust web server domain, then you can do the same thing as case 1. 否则,当您可能需要IT管理员使文件服务器域信任Web服务器域时,则可以执行与情况1相同的操作。

About impersonation code, it could be something like: 关于模拟代码,可能类似于:

    //get the identity of an appPool 
         using(System.Security.Principal.WindowsIdentity wid = System.Security.Principal.WindowsIdentity.GetCurrent())
         {
             using (System.Security.Principal.WindowsImpersonationContext ImpersonationCtx = wid.Impersonate())
             {
                 //creating folders, uploading files to UNC path... 

                 ImpersonationCtx.Undo();
             }
         }

If your case is one of this, hope it helps. 如果您的情况就是其中之一,希望对您有所帮助。

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

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