3

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.

  • What kind of tool are you trying to create? Is it a web application or a desktop application? – Gaurav Mantri May 19 '17 at 08:26
  • I want to auto scale the Azure Search component based on metrics. As of right now Azure’s auto scaling tool, only supports a handful of components … Azure Search is not one of them. – Diogo Fernandes May 19 '17 at 08:26
  • Let me ask again :) - Is the tool you're building a web application or a desktop application. You're passing some incorrect parameters and the correct value will depend on your answer. – Gaurav Mantri May 19 '17 at 08:32
  • @GauravMantri Is there a difference? If there is, my tendency is Web Application – Diogo Fernandes May 19 '17 at 08:33
  • @GauravMantri Right now it's a Console Application (to fool around), which parameters are wrong? – Diogo Fernandes May 19 '17 at 08:35
  • @DiogoFernandes Side question, are you using the Azure Search Management SDK for .NET? It's not clear from the title of your question. It's easier than using the Management REST API directly. The NuGet package is here: https://www.nuget.org/packages/Microsoft.Azure.Management.Search – Bruce Johnston May 19 '17 at 20:59

1 Answers1

2

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/.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Many thanks! With this, I can hopefully try some things out. I saw that in Azure AD (in the Register App) there is an option for “native client”. Would this also apply for the sample you wrote? Or do I need to do something entirely different? – Diogo Fernandes May 19 '17 at 09:04
  • Since you're building a console app, you would use native app. Please give it a try and update your question with any problems that you may encounter. If I have an answer to those, I'll update my answer accordingly. – Gaurav Mantri May 19 '17 at 09:17
  • I got it running and got a valid token. Thanks a lot! – Diogo Fernandes May 19 '17 at 10:58