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

1

0

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
}}
Sula answered 10/10, 2020 at 4:24 Comment(0)
U
0

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.

Undersize answered 10/10, 2020 at 7:22 Comment(2)
Your suggestion makes sense. But per your suggestion when I tried with the global admin account for my Azure subscription. I still get the same error with an additional sentence: The 'Authorization' header is provided in an invalid format. The rest of the error message is same as shown in Error Details section of my post above. Any thoughts/suggestions?Sula
According to learn.microsoft.com/en-us/rest/api/azure/… you need to obtain a token to add to the request (not a coding or API expert by any means) but using OAuth seems to need this 2 step process rather than your attempt with Basic Auth?Undersize

© 2022 - 2024 — McMap. All rights reserved.