0
votes

I am using the following adal library in order to fetch Oauth2 token (AAD).

https://github.com/AzureAD/azure-activedirectory-library-for-python

The following code does work and i can get my token: https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/certificate_credentials_sample.py

But in my specific case i want to be able to fetch the token with providing username and password (password grant type)

Any ideas?

Thanks,

2

2 Answers

1
votes

It's not well documented but user/password authentication is still available in latest (0.4.4)

context = adal.AuthenticationContext('https://login.microsoftonline.com/common')
context.acquire_token_with_username_password(
    'https://management.core.windows.net',
    'me@outlook.com',
    'password',
    '04b07795-8ddb-461a-bbee-02f9e1bf7b46')

See for reference: https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/adal/authentication_context.py#L128-L145

0
votes

The library for some reason removed possibility to perform authentication using username password, in latest version.

however use 3.13.8.999 this version of the Microsoft.IdentityModel.Clients.ActiveDirectory nuget package, which is not the latest but has exactly what you need.

and then just go like this

string tenantname = ConfigurationManager.AppSettings["ida:Tenant"];
string clientId = ConfigurationManager.AppSettings["ida:ClientID"];

string authority = $"https://login.microsoftonline.com/{tenantname}";
var authenticationContext = new AuthenticationContext(authority, null);

string username = $"{UserName}@{tenantname}";
var userCred = new UserPasswordCredential(username, Password);

 AuthenticationResult authResult = await   authenticationContext.AcquireTokenAsync(clientId, clientId, userCred);

Hope this is what you are looking for.