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

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

问题描述

最近通过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>

 

参考资料

禁用基本身份验证:https://docs.azure.cn/zh-cn/app-service/deploy-configure-credentials?tabs=cli#disable-basic-authentication

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

相关文章
|
5天前
|
API
【Azure Logic App】使用Logic App来定制Monitor Alert邮件内容遇见无法获取SearchResults的情况
Log search alert rules from API version 2020-05-01 use this payload type, which only supports common schema. Search results aren't embedded in the log search alerts payload when you use this version.
30 10
|
1月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
43 11
|
1月前
|
JavaScript C++ 容器
【Azure Bot Service】部署NodeJS ChatBot代码到App Service中无法自动启动
2024-11-12T12:22:40.366223350Z Error: Cannot find module 'dotenv' 2024-11-12T12:40:12.538120729Z Error: Cannot find module 'restify' 2024-11-12T12:48:13.348529900Z Error: Cannot find module 'lodash'
43 11
|
1月前
|
缓存 容器 Perl
【Azure Container App】Container Apps 设置延迟删除 (terminationGracePeriodSeconds) 的解释
terminationGracePeriodSeconds : 这个参数的定义是从pod收到terminated signal到最终shutdown的最大时间,这段时间是给pod中的application 缓冲时间用来处理链接关闭,应用清理缓存的;并不是从idel 到 pod被shutdown之间的时间;且是最大时间,意味着如果application 已经gracefully shutdown,POD可能被提前terminated.
|
1月前
|
开发框架 监控 .NET
【Azure App Service】部署在App Service上的.NET应用内存消耗不能超过2GB的情况分析
x64 dotnet runtime is not installed on the app service by default. Since we had the app service running in x64, it was proxying the request to a 32 bit dotnet process which was throwing an OutOfMemoryException with requests >100MB. It worked on the IaaS servers because we had the x64 runtime install
|
1月前
|
Java 开发工具 Windows
【Azure App Service】在App Service中调用Stroage SDK上传文件时遇见 System.OutOfMemoryException
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
|
1月前
|
安全 Apache 开发工具
【Azure App Service】在App Service上关于OpenSSH的CVE2024-6387漏洞解答
CVE2024-6387 是远程访问漏洞,攻击者通过不安全的OpenSSh版本可以进行远程代码执行。CVE-2024-6387漏洞攻击仅应用于OpenSSH服务器,而App Service Runtime中并未使用OpenSSH,不会被远程方式攻击,所以OpenSSH并不会对应用造成安全风险。同时,如果App Service的系统为Windows,不会受远程漏洞影响!
|
SQL 网络安全 Windows
|
2月前
|
小程序 JavaScript 前端开发
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
750 1