File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed
Services/UserManagement/Avatar/UrlGenerators
test/Discussion.Web.Tests/Specs/Services Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff line change 1
1
using Discussion . Core . Models . UserAvatar ;
2
+ using Microsoft . AspNetCore . Http ;
2
3
using Microsoft . AspNetCore . Mvc ;
3
4
4
5
namespace Discussion . Web . Services . UserManagement . Avatar . UrlGenerators
5
6
{
6
7
public class StorageFileAvatarUrlGenerator : IUserAvatarUrlGenerator < StorageFileAvatar >
7
8
{
8
9
private readonly IUrlHelper _urlHelper ;
10
+ private readonly IHttpContextAccessor _httpContextAccessor ;
9
11
10
- public StorageFileAvatarUrlGenerator ( IUrlHelper urlHelper )
12
+ public StorageFileAvatarUrlGenerator ( IUrlHelper urlHelper , IHttpContextAccessor httpContextAccessor )
11
13
{
12
14
_urlHelper = urlHelper ;
15
+ _httpContextAccessor = httpContextAccessor ;
13
16
}
14
17
15
18
public string GetUserAvatarUrl ( StorageFileAvatar avatar )
16
19
{
20
+ var request = _httpContextAccessor . HttpContext . Request ;
17
21
// ReSharper disable Mvc.ActionNotResolved
18
22
// ReSharper disable Mvc.ControllerNotResolved
19
23
return _urlHelper . Action ( "DownloadFile" ,
20
24
"Common" ,
21
25
new { slug = avatar . StorageFileSlug } ,
22
- _urlHelper . ActionContext . HttpContext . Request . Scheme ) ;
26
+ request . Scheme ,
27
+ request . Host . ToString ( ) ) ;
23
28
}
24
29
}
25
30
}
Original file line number Diff line number Diff line change 28
28
using Discussion . Core . Logging ;
29
29
using Discussion . Core . Middleware ;
30
30
using Discussion . Core . Utilities ;
31
+ using Microsoft . AspNetCore . HttpOverrides ;
31
32
32
33
namespace Discussion . Web
33
34
{
@@ -57,6 +58,12 @@ private static void Main()
57
58
// ConfigureServices is invoked before Configure
58
59
public void ConfigureServices ( IServiceCollection services )
59
60
{
61
+ services . Configure < ForwardedHeadersOptions > ( options =>
62
+ {
63
+ options . ForwardedHeaders = ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto | ForwardedHeaders . XForwardedHost ;
64
+ options . KnownNetworks . Clear ( ) ;
65
+ options . KnownProxies . Clear ( ) ;
66
+ } ) ;
60
67
services . AddLazySupport ( ) ;
61
68
services . AddLogging ( ) ;
62
69
services . AddSingleton ( HtmlEncoder . Create ( UnicodeRanges . BasicLatin , UnicodeRanges . CjkUnifiedIdeographs ) ) ;
@@ -107,6 +114,7 @@ public void ConfigureServices(IServiceCollection services)
107
114
108
115
public void Configure ( IApplicationBuilder app )
109
116
{
117
+ app . UseForwardedHeaders ( ) ;
110
118
app . UseTracingId ( ) ;
111
119
SetupGlobalExceptionHandling ( app ) ;
112
120
SetupHttpsSupport ( app ) ;
Original file line number Diff line number Diff line change @@ -80,9 +80,12 @@ private static IUrlHelper CreateMockUrlHelper(string slug = null)
80
80
private static IServiceProvider CreateServices ( IUrlHelper urlHelper )
81
81
{
82
82
var services = new ServiceCollection ( ) ;
83
+ var mockAccessor = new Mock < IHttpContextAccessor > ( ) ;
84
+ mockAccessor . SetupGet ( m => m . HttpContext ) . Returns ( new DefaultHttpContext ( ) ) ;
83
85
84
86
services . AddScoped ( sp => urlHelper ) ;
85
87
services . AddSingleton < IAvatarUrlService , DispatchAvatarUrlService > ( ) ;
88
+ services . AddSingleton ( mockAccessor . Object ) ;
86
89
services . AddScoped < IUserAvatarUrlGenerator < DefaultAvatar > , DefaultAvatarUrlGenerator > ( ) ;
87
90
services . AddScoped < IUserAvatarUrlGenerator < StorageFileAvatar > , StorageFileAvatarUrlGenerator > ( ) ;
88
91
services . AddScoped < IUserAvatarUrlGenerator < GravatarAvatar > , GravatarAvatarUrlGenerator > ( ) ;
You can’t perform that action at this time.
0 commit comments