Skip to content

Commit 5bbe0ff

Browse files
committed
Add CustomRedirectHandler to handle 307 redirects
1 parent d194051 commit 5bbe0ff

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

CSharp/Library/Microsoft.Bot.Connector.NetCore/Content/Microsoft.Bot.Connector.NetCore.XML

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CSharp/Library/Microsoft.Bot.Connector.NetFramework/Content/Microsoft.Bot.Connector.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CSharp/Library/Microsoft.Bot.Connector.Shared/ConnectorClientEx.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,32 @@ public ConnectorClient(Uri baseUri, string microsoftAppId = null, string microso
3131
/// <param name="addJwtTokenRefresher">True, if JwtTokenRefresher should be included; False otherwise.</param>
3232
/// <param name="handlers">Optional. The delegating handlers to add to the http client pipeline.</param>
3333
public ConnectorClient(Uri baseUri, MicrosoftAppCredentials credentials, bool addJwtTokenRefresher = true, params DelegatingHandler[] handlers)
34-
: this(baseUri, addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials) : handlers)
34+
: this(baseUri, addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials): handlers)
3535
{
3636
this.Credentials = credentials;
3737
}
3838

39+
/// <summary>
40+
/// Create a new instances of the ConnectorClient.
41+
/// </summary>
42+
/// <param name="baseUri">Base URI for the Connector service</param>
43+
/// <param name="credentials">Credentials for the Connector service</param>
44+
/// <param name="httpClientHandler">The httpClientHandler used by http client</param>
45+
/// <param name="addJwtTokenRefresher">True, if JwtTokenRefresher should be included; False otherwise.</param>
46+
/// <param name="handlers">Optional. The delegating handlers to add to the http client pipeline.</param>
47+
public ConnectorClient(Uri baseUri, MicrosoftAppCredentials credentials, HttpClientHandler httpClientHandler, bool addJwtTokenRefresher = true, params DelegatingHandler[] handlers)
48+
: this(baseUri, httpClientHandler, addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials) : handlers)
49+
{
50+
this.Credentials = credentials;
51+
}
52+
3953
private static DelegatingHandler[] AddJwtTokenRefresher(DelegatingHandler[] srcHandlers, MicrosoftAppCredentials credentials)
4054
{
4155
var handlers = new List<DelegatingHandler>(srcHandlers);
4256
handlers.Add(new JwtTokenRefresher(credentials));
4357
return handlers.ToArray();
4458
}
45-
59+
4660
// client defaults to sending the expect: continue header, which isn't very efficient,
4761
partial void CustomInitialize()
4862
{

CSharp/Library/Microsoft.Bot.Connector.Shared/JwtTokenRefresher.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,26 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
6161
return response;
6262
}
6363
}
64+
65+
/// <summary>
66+
/// A custom redirect handler for <see cref="HttpStatusCode.RedirectKeepVerb"/>.
67+
/// </summary>
68+
/// <remarks>
69+
/// This makes sure that authorization headers stay intact between 307 redirects.
70+
/// </remarks>
71+
public sealed class CustomRedirectHandler : DelegatingHandler
72+
{
73+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
74+
{
75+
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
76+
77+
if(response.StatusCode == HttpStatusCode.RedirectKeepVerb && response.Headers.Contains("Location"))
78+
{
79+
request.RequestUri = new Uri(request.RequestUri, response.Headers.Location);
80+
response.Dispose();
81+
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
82+
}
83+
return response;
84+
}
85+
}
6486
}

0 commit comments

Comments
 (0)