-
Notifications
You must be signed in to change notification settings - Fork 976
Make ITokenCredentialProvider public #17387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Eric Erhardt (eerhardt)
merged 1 commit into
main
from
public-token-credential-provider
May 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Azure.Core; | ||
|
|
||
| namespace Aspire.Hosting.Azure; | ||
|
|
||
| /// <summary> | ||
| /// Provides access to the <see cref="TokenCredential"/> that Aspire uses | ||
| /// to authenticate against Azure when provisioning resources and calling Azure APIs. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This service is registered as a singleton when Azure provisioning is enabled | ||
| /// (e.g., by <c>AddAzureProvisioning</c>). | ||
| /// </para> | ||
| /// <para> | ||
| /// Integrations and app host code can resolve this service from <see cref="System.IServiceProvider"/> | ||
| /// to obtain a <see cref="TokenCredential"/> instance configured by Aspire's | ||
| /// Azure provisioning options (for example, the configured tenant id and credential source). | ||
| /// The concrete credential type returned by <see cref="TokenCredential"/> is an implementation | ||
| /// detail and may change between releases; callers should treat the value as an opaque | ||
| /// <see cref="TokenCredential"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface ITokenCredentialProvider | ||
| { | ||
| /// <summary> | ||
| /// Gets the <see cref="TokenCredential"/> to use for Azure authentication. | ||
| /// </summary> | ||
| TokenCredential TokenCredential { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/Aspire.Hosting.Azure.Tests/TokenCredentialProviderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Utils; | ||
| using Azure.Core; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Aspire.Hosting.Azure.Tests; | ||
|
|
||
| public class TokenCredentialProviderTests | ||
| { | ||
| [Fact] | ||
| public void AddAzureProvisioning_RegistersITokenCredentialProvider() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
| builder.AddAzureProvisioning(); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| var provider = app.Services.GetRequiredService<ITokenCredentialProvider>(); | ||
|
|
||
| Assert.NotNull(provider); | ||
| Assert.NotNull(provider.TokenCredential); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddAzureProvisioning_RegistersITokenCredentialProviderAsSingleton() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
| builder.AddAzureProvisioning(); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| var first = app.Services.GetRequiredService<ITokenCredentialProvider>(); | ||
| var second = app.Services.GetRequiredService<ITokenCredentialProvider>(); | ||
|
|
||
| Assert.Same(first, second); | ||
| Assert.Same(first.TokenCredential, second.TokenCredential); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddingAzureResource_RegistersITokenCredentialProvider() | ||
| { | ||
| // AddAzureProvisioning is invoked indirectly when an Azure resource is added. | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
| builder.AddAzureInfrastructure("infra", _ => { }); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| var provider = app.Services.GetRequiredService<ITokenCredentialProvider>(); | ||
|
|
||
| Assert.NotNull(provider.TokenCredential); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ITokenCredentialProvider_CanBeReplacedWithCustomImplementation() | ||
| { | ||
| // External callers should be able to plug in their own credential by | ||
| // replacing the registered service with their own implementation. | ||
| var customCredential = new TestTokenCredential(); | ||
| var customProvider = new CustomTokenCredentialProvider(customCredential); | ||
|
|
||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
| builder.AddAzureProvisioning(); | ||
| builder.Services.AddSingleton<ITokenCredentialProvider>(customProvider); | ||
|
|
||
| using var app = builder.Build(); | ||
|
|
||
| var resolved = app.Services.GetRequiredService<ITokenCredentialProvider>(); | ||
|
|
||
| Assert.Same(customProvider, resolved); | ||
| Assert.Same(customCredential, resolved.TokenCredential); | ||
| } | ||
|
|
||
| private sealed class CustomTokenCredentialProvider(TokenCredential credential) : ITokenCredentialProvider | ||
| { | ||
| public TokenCredential TokenCredential { get; } = credential; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.