调用Azure SQL数据库REST API会出现错误:不支持Basic的身份验证方案

问题描述

在我的WPF Core客户端应用程序的以下代码中,我试图使用此Azure SQL Database REST API删除Azure订阅中的数据库。但我收到如下所示的错误

问题:我可能遗失的东西以及如何解决该问题?

备注:我找到了一个类似的示例here,但不确定为什么我的代码无法正常工作。当我从笔记本电脑上的SSMS - 2019连接到相同的Azure sql Db时,相同的用户名(具有sql Admin访问权限)和密码可以正常工作。

代码

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帐户进行身份验证。