【Azure App Service】通过Visual Studio部署Azure App Service 遇见 401 'Unauthorized'错误

azure,app,service,通过,visual,studio,部署,遇见,unauthorized,错误 · 浏览次数 : 30

小编点评

**问题描述:** 当通过 Visual Studio 2022 部署 Azure App Service 时,出现 401 Unauthorized 错误,提示 "Web deployment task failed. (Connected to the remote computer (\"javatest02.scm.chinacloudsites.cn\") using the Web Management Service, but could not authorize.)" **错误信息:** ``` Error : Web deployment task failed. (Connected to the remote computer (\"javatest02.scm.chinacloudsites.cn\") using the Web Management Service, but could not authorize. Make sure that you are using the correct user name and password, that the site you are connecting to exists, and that the credentials represent a user who has permissions to access the site. Learn more at: https://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.) ``` **问题解决方法:** 1. **检查 App Service 基本设置 (General Setting):** - 在 App Service 中,导航到 "基础设置" (General Setting)。 - 在 "安全" 中,确保 "Basic Authentication" 设置为 "Off"。 2. **重新部署 App Service:** - 使用 Visual Studio 2022 中的 "部署" 功能重新部署应用程序。 - 请确保 "Basic Authentication" 设置为 "On"。 3. **禁用 Basic Authentication:** - 您可以禁用 App Service 的 Basic Authentication 功能,并使用 Azure AD 进行身份验证。 - 请参阅文档以进行设置。 4. **使用 Azure AD 身份验证:** - 您可以在 VS 中使用 Azure AD 身份验证来部署应用程序。 - 请参阅文档以进行设置。 **其他提示:** - 确保您使用正确的用户名和密码。 - 检查您连接的远程计算机的防火墙规则,确保允许 Web Management Service 连接。 - 如果您仍然遇到问题,请咨询 Azure 的官方支持团队。

正文

问题描述

最近通过Visual Studio 2022部署Azure App Service的时候,突然遇见了部署失败, 401 Unauthorized错误。 

 错误消息:

Build started...
1>------ Build started: Project: myapi01, Configuration: Release Any CPU ------
1>myapi01 -> C:\MyCode\44-AppService-ASP.NET\myapi01\bin\Release\net7.0\myapi01.dll
2>------ Publish started: Project: myapi01, Configuration: Release Any CPU ------
myapi01 -> C:\MyCode\44-AppService-ASP.NET\myapi01\bin\Release\net7.0\myapi01.dll
myapi01 -> C:\MyCode\44-AppService-ASP.NET\myapi01\obj\Release\net7.0\PubTmp\Out\
C:\Program Files\dotnet\sdk\7.0.304\Sdks\Microsoft.NET.Sdk.Publish\targets\PublishTargets\Microsoft.NET.Sdk.Publish.MSDeploy.targets(140,5): 
Error : Web deployment task failed. (Connected to the remote computer ("javatest02.scm.chinacloudsites.cn") using the Web Management Service,
but could not authorize. Make sure that you are using the correct user name and password, that the site you are connecting to exists,
and that the credentials represent a user who has permissions to access the site.
Learn more at: https://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.) Make sure the site name, user name, and password are correct. If the issue is not resolved, please contact your local or server administrator. Error details: Connected to the remote computer ("javatest02.scm.chinacloudsites.cn") using the Web Management Service, but could not authorize.
Make sure that you are using the correct user name and password, that the site you are connecting to exists,
and that the credentials represent a user who has permissions to access the site.
Learn more at: https://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED. The remote server returned an error: (401) Unauthorized. Publish failed to deploy. 2>Build failed. Check the Output window for more details. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== ========== Build started at 7:10 PM and took 04.863 seconds ========== ========== Publish: 0 succeeded, 1 failed, 0 skipped ========== ========== Publish started at 7:10 PM and took 04.863 seconds ==========

非常疑惑,因为在VS中使用的认证是从App Service门户上下载的Publish Profile文件,其中包含的登录的用户名和密码。 

那为什么它就不能通过App Service的登录认证了呢?

 

问题解答

因为发布中使用的Publish Profile文件是从Azure 门户上下载,不存在人为错误导致密码错误无法认证。那么就只能从配置方面着手检查:

第一步:查看App Service的基本设置(General Setting)

注:Basic Authentication被设置为 Off。表示通过站点的用户名和密码登录方式已经不可用。 此点正符合部署时遇见的401 Unauthorized问题。

 

第二步: 修改 Basic Authentication为On

再次从VS 2022中部署站点,发布成功。

当然,如果不启用 Basic Authentication,有没有其他可以部署的方式呢?

 

当然可以,使用Azure AD登录认证,也就是我们最常规的使用登录Azure 的账号进行认证。

禁用基本身份验证

一些组织需要满足安全要求,因此宁愿禁用通过 FTP 或 WebDeploy 进行的访问。 这样一来,组织的成员就只能通过 Azure Active Directory (Azure AD) 控制的 API 访问其应用服务。

Link: https://docs.azure.cn/zh-cn/app-service/deploy-configure-credentials?tabs=cli#disable-basic-authentication 

如使用az cli命令进行部署:

az cloud set --name AzureChinaCloud
 

az login -u <Azure User Account> -p <Password>


az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"


az webapp deploy --resource-group <group-name> --name <app-name> --src-path <zip-package-path>

 

参考资料

部署App Service: https://docs.azure.cn/zh-cn/app-service/deploy-zip?tabs=cli 

 

与【Azure App Service】通过Visual Studio部署Azure App Service 遇见 401 'Unauthorized'错误相似的内容:

【Azure App Service】通过Visual Studio部署Azure App Service 遇见 401 'Unauthorized'错误

Error : Web deployment task failed. (Connected to the remote computer ("javatest02.scm.chinacloudsites.cn") using the Web Management Service, but could not authorize. Make sure that you are using the

【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.

问题描述 PHP的Web Job,通过artisan来配置路径启动PHP任务,相关启动脚本如下: artisan_path = "d:\\home\\site\\wwwroot"; cd ${artisan_path} echo "\n" pwd php artisan schedule:run 但

【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器

问题描述 在App Service for linux上创建一个PHP应用,通过 phpinfo() 查看PHP的扩展设置,发现JIT没有被开启, jit_buffer_size 大小为0. 那么,在App Service的环境中,如何开启JIT呢? 问题解答 PHP 8在PHP的内核中添加了JIT

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)

在前一篇文章中,我们是把.NET 8应用读取SSL证书(X509)示例部署在App Service Windows环境中,那么如果部署在Linux环境,以及Linux Container中呢? 根据前文中的第一种方法,直接在把证书文件包含在源文件中,通过相对路径读取证书文件的方式,经测试,可以正常工

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)

在使用App Service服务部署业务应用,因为有些第三方的接口需要调用者携带TLS/SSL证书(X509 Certificate),在官方文档中介绍了两种方式在代码中使用证书: 1) 直接使用证书文件路径加载证书 new X509Certificate2 2) 从系统的证书库中通过指纹加载...

【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值

问题描述 App Service中,如何通过 Application Setting 来配置 Key Vault中的值呢? 问题解答 首先,App Service服务可以直接通过引用的方式,无需代码的情况下,为Application Setting中的Key配置Key Vault中保存的值。参考官方

【Azure 应用服务】App Servie网站报403 ModSecurity Action错误

问题描述 App Service 部署应用程序,然后通过App Gateway(WAF) 提供公网访问,但是一直遇见403报错,刷新页面,回退,重新Web页面能缓解403问题。 问题分析 通过浏览器F12抓取网络日志(Network Trace)来定位403返回的情况,发现请求返回的Status为 

【Azure 应用服务】部署WAR包到App Service访问出现404错误的解决方式

问题描述 在Linux的App Service上,通过FTP把war文件和HTML静态文件上传到wwwroot目录下,静态文件访问成功,但是java应用中的请求都返回404错误 问题解决 因为FTP上传文件只是把文件放在 WWWROOT 目录中,并没有部署war包成功。如果要部署war包,需要使用w

【Azure 应用服务】App Service的运行状况检查功能失效,一直提示"实例运行不正常"

问题描述 为App Service配置了健康检查,单独访问Health Check Path的路径,返回代码为200。但为什么在App Service的页面上,一直提示“实例运行不正常”呢? 问题解答 通过查看Health Check Path发送的请求,当使用HTTPS的时候,直接返回200,而当

【Azure 应用服务】 在App Service中无法上传证书[Private Key Certificates (.pfx)],导入Azure Key Vault中的证书也无法成功

问题描述 在App Service的TLS/SSL settings页面,切换到Private Key Certificates (.pfx),通过Import Key Vault Certificate方式上传证书,提示成功,实际没有上传成功。通过Upload Certificate的方式上传证书