-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[App Configuration] - Add audience error handling policy #53834
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
zhiyuanliang-ms
merged 11 commits into
Azure:main
from
zhiyuanliang-ms:zhiyuanliang/audience-error
Nov 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
530f325
add Audience error handling policy
zhiyuanliang-ms b40d5c6
update
zhiyuanliang-ms e608aaa
not introduce Azure.Identity
zhiyuanliang-ms 8d5d15f
update
zhiyuanliang-ms 4fbea4e
update
zhiyuanliang-ms 56503a5
update
zhiyuanliang-ms 414f6c4
add test
zhiyuanliang-ms 89f2706
Revert "add test"
zhiyuanliang-ms 16d06ee
add unit test
zhiyuanliang-ms b8ee403
update
zhiyuanliang-ms 1743786
update
zhiyuanliang-ms 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
65 changes: 65 additions & 0 deletions
65
sdk/appconfiguration/Azure.Data.AppConfiguration/src/AudienceErrorHandlingPolicy.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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core; | ||
| using Azure.Core.Pipeline; | ||
| using Azure.Identity; | ||
|
|
||
| namespace Azure.Data.AppConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Pipeline policy that inspects <see cref="AuthenticationFailedException"/> instances for the Entra ID token audience error code (AADSTS500011) and rethrows a new exception with clearer guidance. | ||
| /// </summary> | ||
| internal class AudienceErrorHandlingPolicy : HttpPipelinePolicy | ||
| { | ||
| private bool _isAudienceConfigured; | ||
zhiyuanliang-ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private const string AadAudienceErrorCode = "AADSTS500011"; | ||
zhiyuanliang-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private const string NoAudienceErrorMessage = "Unable to authenticate to Azure App Configuration. No authentication token audience was provided. Please set ConfigurationClientOptions.Audience to the appropriate audience for the target cloud. For details on how to configure the authentication token audience visit https://aka.ms/appconfig/client-token-audience."; | ||
zhiyuanliang-ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private const string WrongAudienceErrorMessage = "Unable to authenticate to Azure App Configuration. An incorrect token audience was provided. Please set ConfigurationClientOptions.Audience to the appropriate audience for the target cloud. For details on how to configure the authentication token audience visit https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| public AudienceErrorHandlingPolicy(bool isAudienceConfigured) | ||
| { | ||
| _isAudienceConfigured = isAudienceConfigured; | ||
| } | ||
|
|
||
| public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| try | ||
| { | ||
| ProcessNext(message, pipeline); | ||
| } | ||
| catch (AuthenticationFailedException ex) | ||
| { | ||
| HandleAuthenticationAudienceError(ex); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| try | ||
| { | ||
| await ProcessNextAsync(message, pipeline).ConfigureAwait(false); | ||
| } | ||
| catch (AuthenticationFailedException ex) | ||
| { | ||
| HandleAuthenticationAudienceError(ex); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| private void HandleAuthenticationAudienceError(AuthenticationFailedException ex) | ||
| { | ||
| // Message string matching is used because AAD error codes are embedded in the exception message. | ||
| if (!ex.Message.Contains(AadAudienceErrorCode)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string message = _isAudienceConfigured ? WrongAudienceErrorMessage : NoAudienceErrorMessage; | ||
| throw new AuthenticationFailedException(message, ex); | ||
zhiyuanliang-ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
zhiyuanliang-ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
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
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.