首页 > 解决方案 > 调用 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
}}

标签: c#azureazure-sql-databaseazure-sql-serverazure-rest-api

解决方案


我认为您可能混淆了身份验证。

使用 SSMS 连接时,您将连接到数据库并在数据库中进行身份验证。

若要删除数据库,您需要使用具有删除 Azure SQL 数据库的足够权限的 Azure AD 帐户连接到 Azure 租户。

尝试使用您的 Azure 帐户进行身份验证。


推荐阅读