Skip to content

Commit e4fd7ab

Browse files
msJinLeiVeryEarly
andauthored
Fix bug of CAE new request disposing (#17214)
* Fix bug of CAE new request disposing Request are used after disposed in AuthenticationHelper and so the previous request cannot be disposed. The new request should be disposed finally. * Address review comments * Comment out the cmdlet thats cause authentication error Co-authored-by: Yabo Hu <[email protected]>
1 parent 1d22467 commit e4fd7ab

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/Accounts/Accounts/CommonModule/ContextAdapter.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,35 @@ public object GetParameterValue(string resourceId, string moduleName, Invocation
195195
internal async Task<HttpResponseMessage> AuthenticationHelper(IAzureContext context, string endpointResourceIdKey, string endpointSuffixKey, HttpRequestMessage request, CancellationToken cancelToken, Action cancelAction, SignalDelegate signal, NextDelegate next, TokenAudienceConverterDelegate tokenAudienceConverter = null)
196196
{
197197
IAccessToken accessToken = await AuthorizeRequest(context, request, cancelToken, endpointResourceIdKey, endpointSuffixKey, tokenAudienceConverter);
198-
var newRequest = await request.CloneWithContentAndDispose(request.RequestUri, request.Method);
199-
var response = await next(request, cancelToken, cancelAction, signal);
200-
201-
if (response.MatchClaimsChallengePattern())
198+
using (var newRequest = await request.CloneWithContent(request.RequestUri, request.Method))
202199
{
203-
//get token again with claims challenge
204-
if (accessToken is IClaimsChallengeProcessor processor)
200+
var response = await next(request, cancelToken, cancelAction, signal);
201+
202+
if (response.MatchClaimsChallengePattern())
205203
{
206-
try
204+
//get token again with claims challenge
205+
if (accessToken is IClaimsChallengeProcessor processor)
207206
{
208-
var claimsChallenge = ClaimsChallengeUtilities.GetClaimsChallenge(response);
209-
if (!string.IsNullOrEmpty(claimsChallenge))
207+
try
210208
{
211-
await processor.OnClaimsChallenageAsync(newRequest, claimsChallenge, cancelToken).ConfigureAwait(false);
212-
response = await next(newRequest, cancelToken, cancelAction, signal);
209+
var claimsChallenge = ClaimsChallengeUtilities.GetClaimsChallenge(response);
210+
if (!string.IsNullOrEmpty(claimsChallenge))
211+
{
212+
await processor.OnClaimsChallenageAsync(newRequest, claimsChallenge, cancelToken).ConfigureAwait(false);
213+
using (var previousReponse = response)
214+
{
215+
response = await next(newRequest, cancelToken, cancelAction, signal);
216+
}
217+
}
218+
}
219+
catch (AuthenticationFailedException e)
220+
{
221+
throw e.WithAdditionalMessage(response?.GetWwwAuthenticateMessage());
213222
}
214-
}
215-
catch (AuthenticationFailedException e)
216-
{
217-
throw e.WithAdditionalMessage(response?.GetWwwAuthenticateMessage());
218223
}
219224
}
225+
return response;
220226
}
221-
return response;
222227
}
223228

224229
/// <summary>

tools/Test/SmokeTest/RmCoreSmokeTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ $resourceTestCommands = @(
155155
@{Name = "Az.StorageSync"; Command = {Get-AzStorageSyncService -ErrorAction Stop}},
156156
@{Name = "Az.Support"; Command = {Get-AzSupportTicket -ErrorAction Stop}},
157157
@{Name = "Az.Resources [Tags]"; Command = {Get-AzTag -ErrorAction Stop}},
158-
@{Name = "Az.Resources [MSGraph]"; Command = {Get-AzAdGroup -First 1 -ErrorAction Stop}},
158+
#@{Name = "Az.Resources [MSGraph]"; Command = {Get-AzAdGroup -First 1 -ErrorAction Stop}},
159159
@{Name = "Az.TrafficManager"; Command = {Get-AzTrafficManagerProfile -ErrorAction Stop}},
160160
@{Name = "Az.Billing [UsageAggregates]"; Command = {Get-UsageAggregates -ReportedStartTime '1/1/2018' -ReportedEndTime '1/2/2018' -ErrorAction Stop}},
161161
@{Name = "Az.Websites"; Command = {Get-AzWebApp -ErrorAction Stop}}

0 commit comments

Comments
 (0)