Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/identity/Azure.Identity/src/DeviceCodeCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestC
{
try
{
AuthenticationResult result = await Client.AcquireTokenSilentAsync(requestContext.Scopes, (AuthenticationAccount)Record, async, cancellationToken).ConfigureAwait(false);
AuthenticationResult result = await Client.AcquireTokenSilentAsync(requestContext.Scopes, Record, async, cancellationToken).ConfigureAwait(false);

return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestC
{
try
{
AuthenticationResult result = await Client.AcquireTokenSilentAsync(requestContext.Scopes, (AuthenticationAccount)Record, async, cancellationToken).ConfigureAwait(false);
AuthenticationResult result = await Client.AcquireTokenSilentAsync(requestContext.Scopes, Record, async, cancellationToken).ConfigureAwait(false);

return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn));
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/identity/Azure.Identity/src/MsalPublicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public virtual async ValueTask<AuthenticationResult> AcquireTokenSilentAsync(str
IPublicClientApplication client = await GetClientAsync(async, cancellationToken).ConfigureAwait(false);
return await client.AcquireTokenSilent(scopes, account).ExecuteAsync(async, cancellationToken).ConfigureAwait(false);
}
public virtual async ValueTask<AuthenticationResult> AcquireTokenSilentAsync(string[] scopes, AuthenticationRecord record, bool async, CancellationToken cancellationToken)
{
IPublicClientApplication client = await GetClientAsync(async, cancellationToken).ConfigureAwait(false);

// if the user specified a TenantId when they created the client we want to authenticate to that tenant.
// otherwise we should authenticate with the tenant specified by the authentication record since that's the tenant the
// user authenticated to originally.
return await client.AcquireTokenSilent(scopes, (AuthenticationAccount)record)
.WithAuthority(Pipeline.AuthorityHost.AbsoluteUri, TenantId ?? record.TenantId)
.ExecuteAsync(async, cancellationToken).ConfigureAwait(false);
}

public virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveAsync(string[] scopes, Prompt prompt, bool async, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,23 @@ public async Task AuthenticateWithSharedTokenCacheAsync()

Assert.NotNull(token.Token);
}

[Test]
[Ignore("This test is an integration test which can only be run with user interaction")]
// This test should be run with an MSA account to validate that the refresh for MSA accounts works properly
public async Task AuthenticateWithMSAWithSubsequentSilentRefresh()
{
var cred = new InteractiveBrowserCredential();

// this should pop browser
var authRecord = await cred.AuthenticateAsync();

Assert.NotNull(authRecord);

// this should not pop browser
AccessToken token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" })).ConfigureAwait(false);

Assert.NotNull(token.Token);
}
}
}
12 changes: 12 additions & 0 deletions sdk/identity/Azure.Identity/tests/Mock/MockMsalPublicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public override ValueTask<AuthenticationResult> AcquireTokenSilentAsync(string[]
throw new NotImplementedException();
}

public override ValueTask<AuthenticationResult> AcquireTokenSilentAsync(string[] scopes, AuthenticationRecord record, bool async, CancellationToken cancellationToken)
{
Func<string[], AuthenticationResult> factory = SilentAuthFactory ?? AuthFactory;

if (factory != null)
{
return new ValueTask<AuthenticationResult>(factory(scopes));
}

throw new NotImplementedException();
}

public override ValueTask<AuthenticationResult> AcquireTokenWithDeviceCodeAsync(string[] scopes, Func<DeviceCodeResult, Task> deviceCodeCallback, bool async, CancellationToken cancellationToken)
{
Func<string[], AuthenticationResult> factory = DeviceCodeAuthFactory ?? AuthFactory;
Expand Down