Home > Net >  Acquire Token by service account with MSAL
Acquire Token by service account with MSAL

Time:11-23

we are using ADAL to acquire tokens by a service account silently (no prompt, no redirect). This is our sample code:

var clientId = "";
var tenantDomain = "";
var userName = "";
var password = "";
var context = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantDomain));
var credential = new UserPasswordCredential(userName, password);
var result = await context.AcquireTokenAsync("https://management.core.windows.net/", clientId, credential);

How can I acquire tokens using MSAL?

CodePudding user response:

Use AcquireTokenByUsernamePassword(), see enter image description here

code sample

try
            {
                var securePassword = new SecureString();
                foreach (char c in "yourpassword")        // you should fetch the password
                    securePassword.AppendChar(c);  // keystroke by keystroke

                result = await app.AcquireTokenByUsernamePassword(scopes,"[email protected]", securePassword).ExecuteAsync();


            }
            catch (Exception ex)
            {
                // See details below
            }

You may want to replace it with an SPN secret auth: https://learn.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/aad/service-prin-aad-token

  • Related