I'm trying to authenticate my client using AAD and automate this using a Windows Service. In AAD .NET SDK, There's two methods, AcquireTokenAsync
and AcquireToken
, but i can't use either of these methods, the await call will stay forever with no response, and when i do something like this:
result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
The object returns a status of Waiting for Activation
& Code 31
..
Now, Is there anyway to acquire the token using hardcoded username and password?
My full code:
string hardcodedUsername = "username";
string hardcodedPassword = "password";
string tenant = "[email protected]";
string clientId = "clientId";
string resourceHostUri = "https://management.azure.com/";
string aadInstance = "https://login.microsoftonline.com/{0}";
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
authContext = new AuthenticationContext(authority);
AuthenticationResult result = null;
try
{
result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return result;
I'm trying to get access to Azure API.
UPDATE 1:
I got this in the output when i tried to await
the call, i think this might help:
Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache: Looking up cache for a token... Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache: No matching token was found in the cache Microsoft.IdentityModel.Clients.ActiveDirectory d__0: Sending user realm discovery request to 'https://login.microsoftonline.com/common/UserRealm/username?api-version=1.0' Microsoft.IdentityModel.Clients.ActiveDirectory d__4: User with hash '***' detected as 'Federated'
async
and putawait
in front ofauthContext.AcquireTokenAsync()
method call? – Gaurav Mantripublic AuthenticationResult getAccessToken() { }
– Muhamed AlGhzawi