From 73400f62e01f7dc5b7a825767c8c01c9e1949e58 Mon Sep 17 00:00:00 2001 From: CrustyJew Date: Fri, 17 Jul 2015 09:03:03 -0500 Subject: [PATCH] Reddit UserAgent Option and token fix -Add UserAgent property to RedditAuthenticationOptions -Fix discarded "var request" in RedditAuthenticationHandler and add header to request --- .../Reddit/RedditAuthenticationHandler.cs | 5 +++-- .../Reddit/RedditAuthenticationOptions.cs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Owin.Security.Providers/Reddit/RedditAuthenticationHandler.cs b/Owin.Security.Providers/Reddit/RedditAuthenticationHandler.cs index ebcc42d..f8525e5 100644 --- a/Owin.Security.Providers/Reddit/RedditAuthenticationHandler.cs +++ b/Owin.Security.Providers/Reddit/RedditAuthenticationHandler.cs @@ -75,12 +75,13 @@ namespace Owin.Security.Providers.Reddit body.Add(new KeyValuePair("state", state)); body.Add(new KeyValuePair("scope", string.Join(",", Options.Scope))); var request = new HttpRequestMessage(HttpMethod.Post, TokenEndpoint); + request.Headers.Add("User-Agent", Options.UserAgent); request.Content = new FormUrlEncodedContent(body); // Request the token HttpResponseMessage tokenResponse = - await httpClient.PostAsync(TokenEndpoint, new FormUrlEncodedContent(body)); + await httpClient.SendAsync(request); tokenResponse.EnsureSuccessStatusCode(); string text = await tokenResponse.Content.ReadAsStringAsync(); @@ -92,7 +93,7 @@ namespace Owin.Security.Providers.Reddit // Get the Reddit user HttpRequestMessage userRequest = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint); - userRequest.Headers.Add("User-Agent", "OWIN OAuth Provider"); + userRequest.Headers.Add("User-Agent", Options.UserAgent); userRequest.Headers.Add("Authorization", "bearer " + Uri.EscapeDataString(accessToken) + ""); HttpResponseMessage graphResponse = await httpClient.SendAsync(userRequest, Request.CallCancelled); graphResponse.EnsureSuccessStatusCode(); diff --git a/Owin.Security.Providers/Reddit/RedditAuthenticationOptions.cs b/Owin.Security.Providers/Reddit/RedditAuthenticationOptions.cs index 262968f..d26f1b7 100644 --- a/Owin.Security.Providers/Reddit/RedditAuthenticationOptions.cs +++ b/Owin.Security.Providers/Reddit/RedditAuthenticationOptions.cs @@ -84,6 +84,12 @@ namespace Owin.Security.Providers.Reddit /// public ISecureDataFormat StateDataFormat { get; set; } + /// + /// Gets or sets the User-Agent string sent with token requests to Reddit. + /// Prefered Format is : "<AppName> ( by /u/<username> )" + /// + public string UserAgent { get; set; } + /// /// Initializes a new /// @@ -93,6 +99,7 @@ namespace Owin.Security.Providers.Reddit Caption = Constants.DefaultAuthenticationType; CallbackPath = new PathString("/signin-reddit"); AuthenticationMode = AuthenticationMode.Passive; + UserAgent = "OWIN OAuth Provider"; Scope = new List { "identity",