Diogo Fernandes
Diogo Fernandes

Reputation: 127

Manage AzureSearch via (Rest) API

I’m trying to create a tool which should scale the „Replicas“ and „Partitions“ of an Azure Search component. For that, I read the following article from Microsoft:

https://learn.microsoft.com/en-us/rest/api/

Right now, I am having trouble authenticating against azure to get an AuthToken. Is there a way to do it easier? Alternatively, do you guys have a sample in how to do it?

Here is a sample of my code:

var clientId = "2aaced54873e4a94b6d5518bc815dcb1";
var redirectUri = new Uri("https://thissucks.search.windows.net");
var resource = "resource"; // What exactly should the value be?

var authContext =
    new AuthenticationContext(
        "https://login.windows.net/ba1cb781739c4cdea71c619ccba914e0/oauth2/authorize", new TokenCache());

var result = authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
var result2 = result.Result;

After invoking this, I get an Azure Login screen. After login with valid credentials, I get the following Exception:

System.AggregateException: 'One or more errors occurred.'

InnerException:

AdalServiceException: AADSTS50001: The application named was not found in the tenant named .
This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant.
You might have sent your authentication request to the wrong tenant.

Upvotes: 3

Views: 157

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

So there're a few issues in your code.

First, please make sure that you have followed the steps described here: https://learn.microsoft.com/en-us/rest/api. Once the application is created successfully, you must note down the client id of that application and use that in your code.

Next, please ensure that ba1cb781739c4cdea71c619ccba914e0 is indeed the tenant id. You could also use Azure AD domain name (something.onmicrosoft.com) instead of this GUID type value. So your URL would be https://login.windows.net/something.onmicrosoft.com/oauth2/authorize

Lastly, there are issues with the values for the following parameters:

var redirectUri = new Uri("https://thissucks.search.windows.net");
var resource = "resource"; // What exactly should the value be?

redirectUri is the URI where the Azure AD will redirect once the user is successfully authenticated. For Web Applications it is usually the URL of your website. Please make sure that it matches with the value you provided when creating an application in Azure AD. When Azure AD redirects the user to this URL, it passes a JWT token in code query string parameter using which you get access/refresh token.

resource is the resource for which you're acquiring the token. Since you want to access Resource Manager API, the value here should be https://management.core.windows.net/.

Upvotes: 2

Related Questions