[英]File upload Issue for External user outside the application - ASP.NET MVC Impersonation
[英]How to Upload File using Application Pool identity instead of Logged User Identity- ASP.NET MVC Impersonation
我正在研究 ASP.NET MVC Web 應用程序。 我有一種情況,我想使用應用程序池身份而不是記錄的用戶身份上傳文件。 我只想在上傳文件時使用應用程序池標識。 所有其他地方,我想使用記錄的用戶身份本身。 應用程序托管在兩台服務器上,Server1 和 Server2。Shared 文件夾位於 Server1 中。
目前我正在上傳文件,如下所示
[HttpPost]
public JsonResult Upload()
{
string fileUniqueName = string.Empty;
try
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
string fileName = file.FileName;
fileUniqueName = string.Format("{0}_{1}_{2}",
Path.GetFileNameWithoutExtension(file.FileName),
DateTime.Now.ToString("yyyyMMdd_HHmmss_FFF"),
Path.GetExtension(file.FileName));
string tempFileUploadFolderPath = ConfigurationManager.AppSettings["TempFolderPath"];
Directory.CreateDirectory(tempFileUploadFolderPath);
string fileFullpath = Path.Combine(tempFileUploadFolderPath, fileUniqueName);
file.SaveAs(fileFullpath);
}
}
catch(Exception)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return new JsonResult
{
Data = ""
};
}
return new JsonResult
{
Data = fileUniqueName
};
}
我在 web.config 中有以下設置
<authentication mode="Windows" />
<identity impersonate="true" />
任何人都可以幫助重寫上面的代碼,其中文件上傳適用於應用程序池身份而不是記錄的用戶身份。 文件正在上傳到托管應用程序的文件夾。
看完文檔和實際測試后,我現在可以給你一個詳細的答案。
正如您在上一個線程中提到的,如果非AD組中的用戶希望能夠將文件上傳到文件夾,他們需要以應用程序池身份執行上傳操作。 但是您啟用了模擬,因此當您以非 AD 組用戶身份登錄時,IIS 將使用您登錄的帳戶覆蓋應用程序池標識。 如果 impersonate 設置為 false,則將以應用程序池身份執行上傳,並將文件保存到文件夾中。
但是,如果在整個站點中將impersonate設置為false,則非AD用戶在其他頁面上執行的任何操作也將作為應用程序池標識執行。
所以可以為整個站點設置impersonate為true,只有上傳頁面impersonate為false。 只需 select IIS 中的視圖文件夾,切換到內容視圖,select 一個 cshtml 並右鍵單擊切換到 tatures,然后禁用模擬。 像這樣
一些有用的博客你可以參考: 用戶訪問磁盤
使用 Windows 身份驗證時,應用程序池標識(例如 IIS Apppool\Site001)用於某些訪問,但 Windows 帳戶(例如 User1)用於其他訪問。 這取決於您正在使用的應用程序或框架的模擬設置。 因此,您通常需要授予對應用程序池身份的訪問權限,以及需要訪問您站點的每個 Windows 帳戶(例如 User1、User2、User99)。
您應該能夠使用 web.config 中的<location>
元素控制每個路徑的身份驗證設置。
像這樣的東西:
<location path="/upload">
<system.web>
<authentication mode="Windows"/>
<identity impersonate="false"/>
</system.web>
</location>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.