我正在开发一个 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" />
任何人都可以帮助重写上面的代码,其中文件上传适用于应用程序池身份而不是记录的用户身份。文件正在上传到托管应用程序的文件夹。