Calling Azure SQL Database REST API gives the error: The authentication scheme of Basic is not supported

604 views Asked by At

In the following code of my WPF Core client app , I am trying to use this Azure SQL Database REST API to delete a database in my Azure subscription. But I am getting the error shown below:

Question: What I may be missing, and how can we resolve the issue?

Remark: I found a similar example here, and I'm not sure why my code is not working. The same user name (with SQL Admin access) and password works fine when I connect to the same Azure SQL Db from SSMS - 2019 on my laptop.

Code:

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="The authentication scheme of Basic is not supported."

Error Details:

{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
}}
1

There are 1 answers

2
Martin Cairney On

I think you may be mixing up the authentication.

When connecting using SSMS, you are connecting to the database and authenticating at the database.

To delete the database you need to be connecting to your Azure tenant with an Azure AD account that has sufficient rights to delete an Azure SQL Database.

Try authenticating with your Azure account.