当我之后尝试访问 API 时,使用 powershell 通过 Kudu ZipDeploy 发布到 Azure 会导致 401

Publishing to Azure via Kudu ZipDeploy using powershell causes a 401 when I try to access the API afterward

我有一个 ASP.Net Core 2.1.4 项目,我可以使用“发布”菜单项和“部署到 Azure”类型从 Visual Studio 2017 15.5.7 中部署它。这一直工作得很好。

我正在尝试使用 Bamboo Build/Deployment 服务器部署这个项目,我已经得到了我的 powershell 脚本,可以将 zip 文件中的已发布文件上传到 Kudu Zipdeploy REST 端点,我可以看到部署现在在 Kudu 部署列表中。这是在 David Ebbo 的帮助下 解决的。

但是,现在,当我在使用这种 powershell Kudu 部署方法后尝试使用 Postman 访问 API 时,我得到了 401 未授权状态。

使用 VS2017 发布菜单方法发布完全相同的构建允许我访问 API 就好了,所以我通过 Bamboo 部署和 Powershell 发布的 Kudu 仍然有些不正确。

这是我的 Powershell 函数;

Function Upload-ZipDeploy() {

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $PublishingUsername, $publishingPassword)))
    $userAgent = "powershell/1.0"
    if(!$SlotName) 
    {
        $apiUrl = "https://$WebAppName.scm.azurewebsites.net/api/zipdeploy"
    }
    else {
        $apiUrl = "https://$WebAppName-$SlotName.scm.azurewebsites.net/api/zipdeploy"
    }

    $filePath = $LocalPath
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method Post -InFile $filePath -ContentType "multipart/form-data"

}

我读了一个 MS 论坛 post 谈论 zip 端点并在部署后收到 401 错误,他们通过在部署 URL 上放置尾随 / 来解决它。我用我的脚本试过了,但没有任何区别。

这是日志流中的一个片段,表明部署成功;

2018-04-27T13:34:37    Finished successfully.    
2018-04-27T13:34:37  Running post deployment command(s)...
2018-04-27T13:34:37  Deployment successful.
2018-04-27 13:34:37.160 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 POST http://127.0.0.1:13601/iisintegration  0
2018-04-27 13:34:37.177 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 16.935ms 202 

但是,当我在 Kudu 部署后尝试访问我的 API 端点之一时,我收到 401 未授权状态。

在 Azure 中查看 API 的日志流时,我看到以下内容;

018-04-27 13:37:32.814 +00:00 [Critical] Microsoft.AspNetCore.Hosting.Internal.WebHost: Hosting startup assembly exception
System.InvalidOperationException: Startup assembly 
Microsoft.AspNetCore.AzureKeyVault.HostingStartup failed to execute. See the 
inner exception for more details. ---> System.IO.FileNotFoundException: 
Could not load file or assembly 
'Microsoft.AspNetCore.AzureKeyVault.HostingStartup, Culture=neutral, 
PublicKeyToken=null'. The system cannot find the file specified.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String 
codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, 
StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean 
 throwOnFileNotFound, Boolean forIntrospection, Boolean 
suppressSecurityChecks, IntPtr ptrLoadContextBinder)
   at 
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName 
assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, 
StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean 
throwOnFileNotFound, Boolean forIntrospection, IntPtr ptrLoadContextBinder)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at 

Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices
(AggregateException& hostingStartupErrors)
   --- End of inner exception stack trace ---

至此,我卡住了。我在这里错过了什么?

编辑 #1 2018 年 4 月 30 日

我按照 David Ebbo 的建议添加了站点扩展。我不再在 Azure 日志流中收到文件未找到错误,但在使用 Kupu ZipDeploy 部署后尝试访问我的 API 端点时,我仍然收到 401 未授权错误。同样,如果我使用 VS2017 发布进行部署,我不会收到此错误并且一切正常。

这是我门户上的 Azure 扩展程序屏幕截图。

Azure Portal Site Extensions

编辑 #2 - 2018 年 4 月 30 日

好的,我认为这个问题特别与多目标框架有关。我从需要多目标设置的解决方案中删除了项目,现在我可以在 Bamboo 中使用 ZipDeploy 进行部署,并且在访问端点时我不再获得 401 未授权状态。我相信我可以使用此选项,因为它们是多目标的项目实际上不需要部署到 Azure,因为它们是与数据库相关的实用程序,我可以从以 Azure 数据库为目标的本地服务器 运行 它们。

我想我可能知道你的问题是什么,它与zipdeploy没有直接关系。与旧的核心运行时不同,2.1 未安装在 VM 上(因为它是预览版)。相反,您需要将网站扩展安装到您的 Web 应用程序中。有关详细信息,请参阅 this page(页面是为预览版 1 编写的,但预览版 2 的内容相同)。

我尝试使用干净的 Web 应用程序、VS 中默认的新 Core 2.1 Preview 2 和 zip 部署。安装站点扩展后 运行 正常。