Skip to content

MSAL.NET 2.x to MSAL.NET 3.x

Jean-Marc Prieur edited this page Mar 26, 2019 · 20 revisions

This page explains how to change the code to move from the MSAL 2.x to MSAL 3.x

IEnumerable<string> scopes;
IAccount account;
string authority;
bool forceRefresh;

ClientApplicationBase

AcquireTokenSilent

Used to acquire an access token from the user cache, and refresh it if needed

Instead of use
app.AcquireTokenSilentAsync(scopes,
                            account)
app.AcquireTokenSilent(scopes,
                       account)   
   .ExecuteAsync()
   .ConfigureAwait(false);
app.AcquireTokenSilentAsync(scopes,
                            account, 
                            authority,
                            forceRefresh)
app.AcquireTokenSilent(scopes, account)
   .WithAuthority(authority)
   .WithForceRefresh(forceRefresh)    
   .ExecuteAsync()
   .ConfigureAwait(false);

PublicClientApplication

Constructors of PublicClientApplication

Instead of calling the constructor of PublicClientApplication directly, use the PublicClientApplicationBuilder.Create() or the PublicClientApplicationBuilder.CreateWithOptions() methods. The reference documentation page for PublicClientApplicationBuilder shows all the options that you can use.

Instead of use
app=new PublicClientApplication(clientId);
app=PublicClientApplicationBuilder
    .Create(clientId)
    .Build();
app=new PublicClientApplication(clientId,
                                authority);
app=PublicClientApplicationBuilder
   .Create(clientId)
   .WithAuthority(authority)
   .Build();

Acquire Token interactive

MSAL.NET 2.x had 13 overrides of AcquireTokenAsync

Instead of use
app.AcquireTokenAsync(scopes)
app=AcquireTokenInteractive(scopes, null)
    .ExecuteAsync().
    .ConfigureAwait(false);
app.AcquireTokenAsync(scopes, loginHint)
app=AcquireTokenInteractive(scopes, null)
    .WithLoginHint(loginHint)
    .ExecuteAsync()
    .ConfigureAwait(false);
app.AcquireTokenAsync(scopes, loginHint)
app=AcquireTokenInteractive(scopes, null)
    .WithAccount(account)
    .ExecuteAsync()
    .ConfigureAwait(false);
app.AcquireTokenAsync(scopes,
                      loginHint,
                      uiBehavior,
                      extraQueryParameters)
app=AcquireTokenInteractive(scopes, null)
    .WithLoginHint(account)
    .WithPrompt(prompt)
    .WithExtraQueryParameters(extraQueryParameters)
    .ExecuteAsync()
    .ConfigureAwait(false);
app.AcquireTokenAsync(scopes,
                      loginHint,
                      uiBehavior,
                      extraQueryParameters,
                      extraScopesToConsent,
                      authority)
app=AcquireTokenInteractive(scopes, null)
    .WithLoginHint(loginHint)
    .WithPrompt(prompt)
    .WithExtraQueryParameters(extraQueryParameters)
    .WithExtraSCopesToConsent(extraScopesToConsent)
    .WithAuthority(authority)
    .ExecuteAsync()
    .ConfigureAwait(false);

but of course you only need to specify the parameters that you need

app.AcquireTokenAsync(scopes,
                      account,
                      uiBehavior,
                      extraQueryParameters,
                      extraScopesToConsent,
                      authority)
app=AcquireTokenInteractive(scopes, null)
    .WithAccount(account)
    .WithPrompt(prompt)
    .WithExtraQueryParameters(extraQueryParameters)
    .WithExtraSCopesToConsent(extraScopesToConsent)
    .WithAuthority(authority)
    .ExecuteAsync()
    .ConfigureAwait(false);
app.AcquireTokenAsync(scopes,
                      uiParent)
app=AcquireTokenInteractive(scopes,
                            parentObject)
    .WithUseEmbeddedWebView(useEmbeddedWebView)
    .ExecuteAsync()
    .ConfigureAwait(false);

Getting started with MSAL.NET

Acquiring tokens

Desktop/Mobile apps

Web Apps / Web APIs / daemon apps

Advanced topics

News

FAQ

Other resources

Clone this wiki locally