Skip to content

Commit f250a3e

Browse files
Refactor use of FormUrlEncodedContent
Move the parameters into a local variable before passing to the FormUrlEncodedContent constructor so that it's easier to make nullable annotations happier in .NET 5 (see #438).
1 parent 34c2675 commit f250a3e

File tree

8 files changed

+38
-22
lines changed

8 files changed

+38
-22
lines changed

src/AspNet.Security.OAuth.Fitbit/FitbitAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
var parameters = new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,
7676
["code"] = context.Code
77-
});
77+
};
78+
79+
request.Content = new FormUrlEncodedContent(parameters);
7880

7981
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8082
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
101101
request.Headers.UserAgent.ParseAdd(Options.UserAgent);
102102
}
103103

104-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
104+
var parameters = new Dictionary<string, string>
105105
{
106106
["grant_type"] = "authorization_code",
107107
["redirect_uri"] = context.RedirectUri,
108108
["code"] = context.Code
109-
});
109+
};
110+
111+
request.Content = new FormUrlEncodedContent(parameters);
110112

111113
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
112114
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Shopify/ShopifyAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
179179
using var request = new HttpRequestMessage(HttpMethod.Post, uri);
180180
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
181181

182-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
182+
var parameters = new Dictionary<string, string>
183183
{
184184
["client_id"] = Options.ClientId,
185185
["client_secret"] = Options.ClientSecret,
186186
["code"] = context.Code
187-
});
187+
};
188+
189+
request.Content = new FormUrlEncodedContent(parameters);
188190

189191
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
190192

src/AspNet.Security.OAuth.StackExchange/StackExchangeAuthenticationHandler.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
8585

8686
protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OAuthCodeExchangeContext context)
8787
{
88+
var parameters = new Dictionary<string, string>
89+
{
90+
["client_id"] = Options.ClientId,
91+
["redirect_uri"] = context.RedirectUri,
92+
["client_secret"] = Options.ClientSecret,
93+
["code"] = context.Code,
94+
["grant_type"] = "authorization_code"
95+
};
96+
8897
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint)
8998
{
90-
Content = new FormUrlEncodedContent(new Dictionary<string, string>
91-
{
92-
["client_id"] = Options.ClientId,
93-
["redirect_uri"] = context.RedirectUri,
94-
["client_secret"] = Options.ClientSecret,
95-
["code"] = context.Code,
96-
["grant_type"] = "authorization_code"
97-
})
99+
Content = new FormUrlEncodedContent(parameters)
98100
};
99101

100102
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6868
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
7070

71-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
71+
var parameters = new Dictionary<string, string>
7272
{
7373
["redirect_uri"] = context.RedirectUri,
7474
["client_assertion"] = Options.ClientSecret,
7575
["assertion"] = context.Code,
7676
["grant_type"] = "urn:ietf:params:oauth:grant-type:jwt-bearer",
7777
["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
78-
});
78+
};
79+
80+
request.Content = new FormUrlEncodedContent(parameters);
7981

8082
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
8183

src/AspNet.Security.OAuth.Yahoo/YahooAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
var parameters = new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,
7676
["code"] = context.Code
77-
});
77+
};
78+
79+
request.Content = new FormUrlEncodedContent(parameters);
7880

7981
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8082
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6868
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070

71-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
71+
var parameters = new Dictionary<string, string>
7272
{
7373
["client_id"] = Options.ClientId,
7474
["redirect_uri"] = context.RedirectUri,
7575
["client_secret"] = Options.ClientSecret,
7676
["code"] = context.Code,
7777
["grant_type"] = "authorization_code"
78-
});
78+
};
79+
80+
request.Content = new FormUrlEncodedContent(parameters);
7981

8082
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
8183
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
var parameters = new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,
7676
["code"] = context.Code
77-
});
77+
};
78+
79+
request.Content = new FormUrlEncodedContent(parameters);
7880

7981
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8082
if (!response.IsSuccessStatusCode)

0 commit comments

Comments
 (0)