Reputation: 1270
When I am learning the MSAL library, I met some code that use PublicClientApplication.OperatingSystemAccount
to get an access token, as shown in this code here:
var publicAppBuilder = PublicClientApplicationBuilder
.Create(appClientId)
.WithParentActivityOrWindow(consoleWindowHandleProvider)
.WithAuthority(authorityUri)
.WithBrokerPreview(true)
.WithDefaultRedirectUri()
.Build();
var result = publicAppBuilder.AcquireTokenSilent(scope,
PublicClientApplication.OperatingSystemAccount)
.ExecuteAsync().GetAwaiter().GetResult();
The PublicClientApplication
documentation also mentioned OperatingSystemAccount
as:
A special account value that indicates that the current operating system account should be used to log the user in.
I think the MSAL library code can get the user account from OperatingSystemAccount
, and then use that user account to get the access token of the user from Azure AD, is my reasoning correct?
Upvotes: 0
Views: 49
Reputation: 16054
Yes, when you use OperatingSystemAccount
in your code, MSAL will try to acquire an access token for the currently logged-in user (from the operating system) and use that to authenticate against Azure AD.
var publicAppBuilder = PublicClientApplicationBuilder
.Create(appClientId)
.WithParentActivityOrWindow(consoleWindowHandleProvider)
.WithAuthority(authorityUri)
.WithBrokerPreview(true)
.WithDefaultRedirectUri()
.Build();
var result = publicAppBuilder.AcquireTokenSilent(scope,
PublicClientApplication.OperatingSystemAccount)
.ExecuteAsync().GetAwaiter().GetResult();
AcquireTokenSilent
method is trying to acquire an access token silently using the currently logged-in operating system account. If successful, the access token is returned.How does MSAL get the user credential from that account, what is the implementation details roughly like?
OperatingSystemAccount
is not a special user account on your local machine. It’s a special identifier used within MSAL to indicate that the authentication should happen using the current operating system user’s account (the user who is currently logged into the machine).
OperatingSystemAccount
is not a special local account, but a reference to the authenticated OS user.Upvotes: 0