【问题标题】:UnauthorizedAccessException - Access to the path <>is denied. AWS hosted .NET MVC AppUnauthorizedAccessException - 对路径 <> 的访问被拒绝。 AWS 托管的 .NET MVC 应用程序
【发布时间】:2021-12-23 17:56:28
【问题描述】:

我已将 .NET Core 3.1 应用程序部署到 AWS Elastic Beanstalk(在 IIS 上运行的 Windows Server)。我已经设置了选择文件所需的一切,并且它在本地成功运行。

下面是为访问它而编写的代码。

在startup.cs中(获取宿主环境根位置)

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
       Configuration = configuration;
       SetStaticAttributes(configuration, hostEnvironment);
}


private void SetStaticAttributes(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
       StaticAttributes.RootDirectory = hostEnvironment.ContentRootPath;
}

public IConfiguration Configuration { get; }

在 program.cs 中(试图写入文件)

pdfFilePath = Path.Combine(StaticAttributes.RootDirectory, CommonConstants.PDFFilePath, pdfFimeName);
System.IO.File.WriteAllBytes(pdfFilePath, Convert.FromBase64String(pdf));

当我部署到 AWS 并尝试对其进行测试(使用 Path.Combine)时,我在日志中遇到以下错误。

{
"StatusCode": null,
"SubStatusCode": 0,
"StatusType": null,
"CustomMessage": null,
"InnerException": "null",
"ExceptionType": "DirectoryNotFoundException",
"Message": "Could not find a part of the path 'C:\\wwwroot\\files\\filerepo\\jump.pdf'.",
"BaseException": "null",
"Source": "",
"Request": null

}

当我用 + 连接字符串时,即

在 Program.cs 中

pdfFilePath = StaticAttributes.RootDirectory + CommonConstants.PDFFilePath + pdfFimeName;
System.IO.File.WriteAllBytes(pdfFilePath, Convert.FromBase64String(pdf));

我在日志中遇到以下错误。

{
"StatusCode": null,
"SubStatusCode": 0,
"StatusType": null,
"CustomMessage": null,
"InnerException": "null",
"ExceptionType": "UnauthorizedAccessException",
"Message": "Access to the path 'C:\\inetpub\\AspNetCoreWebApps\\app\\wwwroot\\files\\filerepo\\jump.pdf' is denied.",
"BaseException": "null",
"Source": "",
"Request": null}

请帮忙。

【问题讨论】:

    标签: amazon-web-services .net-core asp.net-core-mvc amazon-elastic-beanstalk


    【解决方案1】:

    最简单的方法是将部署文件放在其他硬盘上,而不是C:/ 驱动器上。喜欢D:/E:/ 驱动器。

    原因:

    因为 C: 盘上的某些文件夹需要更多权限。

    您可以为您的 IIS 站点添加物理路径凭据。

    【讨论】:

    • 嗨,我正在将它部署到 AWS Elastic Beanstalk :/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2019-05-26
    • 1970-01-01
    • 2021-04-02
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多