Skip to content

Commit 9445803

Browse files
CrustyJewkevinchalet
authored andcommitted
Introduce a UserAgent option in the Reddit provider
1 parent a4e29c7 commit 9445803

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
3636
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
3737
request.Headers.Authorization = new AuthenticationHeaderValue("bearer", tokens.AccessToken);
3838

39+
// When a custom user agent is specified in the options, add it to the request headers
40+
// to override the default (generic) user agent used by the OAuth2 base middleware.
41+
if (!string.IsNullOrEmpty(Options.UserAgent))
42+
{
43+
request.Headers.UserAgent.ParseAdd(Options.UserAgent);
44+
}
45+
3946
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
4047
if (!response.IsSuccessStatusCode)
4148
{
@@ -68,7 +75,7 @@ protected override string BuildChallengeUrl(AuthenticationProperties properties,
6875
var address = base.BuildChallengeUrl(properties, redirectUri);
6976

7077
// Add duration=permanent to the authorization request to get an access token that doesn't expire after 1 hour.
71-
// See https://github.com/reddit/reddit/wiki/OAuth2#authorization.
78+
// See https://github.com/reddit/reddit/wiki/OAuth2#authorization for more information.
7279
return QueryHelpers.AddQueryString(address, name: "duration", value: "permanent");
7380
}
7481

@@ -88,6 +95,13 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st
8895
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
8996
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
9097

98+
// When a custom user agent is specified in the options, add it to the request headers
99+
// to override the default (generic) user agent used by the OAuth2 base middleware.
100+
if (!string.IsNullOrEmpty(Options.UserAgent))
101+
{
102+
request.Headers.UserAgent.ParseAdd(Options.UserAgent);
103+
}
104+
91105
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
92106
{
93107
["grant_type"] = "authorization_code",

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,12 @@ public RedditAuthenticationOptions()
2828

2929
Scope.Add("identity");
3030
}
31+
32+
/// <summary>
33+
/// Gets or sets the user agent string to pass when sending requests to Reddit.
34+
/// Setting this option is strongly recommended to prevent request throttling.
35+
/// For more information, visit https://github.com/reddit/reddit/wiki/API.
36+
/// </summary>
37+
public string UserAgent { get; set; }
3138
}
3239
}

0 commit comments

Comments
 (0)