c# - 调用 Azure SQL 数据库 REST API 报错:不支持 Basic 的身份验证方案
问题描述
在我的WPF Core
客户端应用程序的以下代码中,我尝试使用此Azure SQL 数据库 REST API来删除我的 Azure 订阅中的数据库。但我收到如下所示的错误:
问题:我可能缺少什么,我们如何解决这个问题?
备注:我在这里找到了一个类似的例子,我不确定为什么我的代码不起作用。当我从SSMS - 2019
笔记本电脑连接到同一个 Azure SQL Db 时,相同的用户名(具有 SQL 管理员访问权限)和密码可以正常工作。
代码:
const string uri = "https://management.azure.com/subscriptions/a7686c7e8f-211d-45e5-8f5e-525015b1c881/resourceGroups/myResourceGroup/providers/Microsoft.Sql/servers/mysqlserver/databases/AdventureWorksLT2019?api-version=2019-06-01-preview";
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes("mySQLAdminUserName:MySQLAdminPassword");
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
var result = await client.DeleteAsync(uri);
System.Diagnostics.Debug.Write(result.Content.ReadAsStringAsync().Result);
}
error="invalid_token", error_description="不支持Basic的认证方案。"
错误详情:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Cache-Control: no-cache
Pragma: no-cache
WWW-Authenticate: Bearer authorization_uri="https://login.windows.net/96eafd5a-8ce3-4c0c-981c-8eac1f59ab96", error="invalid_token", error_description="The authentication scheme of Basic is not supported."
x-ms-failure-cause: gateway
x-ms-request-id: 43f41f28-5a18-42b7-8e70-d6599996ce0e
x-ms-correlation-request-id: 43f41f28-5a18-42b7-8e70-d6599996ce0e
x-ms-routing-request-id: EASTUS:20201010T035452Z:43f41f28-5a18-42b7-8e70-d6599996ce0e
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Date: Sat, 10 Oct 2020 03:54:51 GMT
Connection: close
Content-Type: application/json; charset=utf-8
Expires: -1
Content-Length: 150
}}
解决方案
我认为您可能混淆了身份验证。
使用 SSMS 连接时,您将连接到数据库并在数据库中进行身份验证。
若要删除数据库,您需要使用具有删除 Azure SQL 数据库的足够权限的 Azure AD 帐户连接到 Azure 租户。
尝试使用您的 Azure 帐户进行身份验证。
推荐阅读
- entity-framework - 如何在更改跟踪器中获取 CascadeDeleted 实体
- flutter - 如何使用 SharedPreferences 在 Flutter 中保存字符串列表
- javascript - Javascript中是否有任何等效的jQuery slice() 方法?
- java - Java类从多个接口继承而不重复代码
- react-native - Geolocation clearWatch(watchId) 不会停止位置跟踪(React Native)
- sqlite - 使用 SQLite 表作为 ItemSource 时,为什么 Xamarin.Forms CollectionView SelectedItems 在 SearchBar 查询后不保持突出显示?
- css - 如何使用 CSS 选择器组排除多个元素?
- azure - 仅将 web.config 和 log4net.config 文件从工件以外的其他存储库文件夹复制到 azure 应用服务
- python - Python-mysql-connector:停止的 mysql 从站上的 cursor.fetch 失败
- java - 尝试使用 javax 邮件依赖项时出错