Skip to content

Commit

Permalink
支持由代理软件设置的 Protocol 与 Hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
Jijie Chen committed Jul 30, 2019
1 parent 85a1aad commit 1cb0998
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using Discussion.Core.Models.UserAvatar;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Discussion.Web.Services.UserManagement.Avatar.UrlGenerators
{
public class StorageFileAvatarUrlGenerator : IUserAvatarUrlGenerator<StorageFileAvatar>
{
private readonly IUrlHelper _urlHelper;
private readonly IHttpContextAccessor _httpContextAccessor;

public StorageFileAvatarUrlGenerator(IUrlHelper urlHelper)
public StorageFileAvatarUrlGenerator(IUrlHelper urlHelper, IHttpContextAccessor httpContextAccessor)
{
_urlHelper = urlHelper;
_httpContextAccessor = httpContextAccessor;
}

public string GetUserAvatarUrl(StorageFileAvatar avatar)
{
var request = _httpContextAccessor.HttpContext.Request;
// ReSharper disable Mvc.ActionNotResolved
// ReSharper disable Mvc.ControllerNotResolved
return _urlHelper.Action("DownloadFile",
"Common",
new {slug = avatar.StorageFileSlug},
_urlHelper.ActionContext.HttpContext.Request.Scheme);
request.Scheme,
request.Host.ToString());
}
}
}
8 changes: 8 additions & 0 deletions src/Discussion.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Discussion.Core.Logging;
using Discussion.Core.Middleware;
using Discussion.Core.Utilities;
using Microsoft.AspNetCore.HttpOverrides;

namespace Discussion.Web
{
Expand Down Expand Up @@ -57,6 +58,12 @@ private static void Main()
// ConfigureServices is invoked before Configure
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
services.AddLazySupport();
services.AddLogging();
services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.CjkUnifiedIdeographs));
Expand Down Expand Up @@ -107,6 +114,7 @@ public void ConfigureServices(IServiceCollection services)

public void Configure(IApplicationBuilder app)
{
app.UseForwardedHeaders();
app.UseTracingId();
SetupGlobalExceptionHandling(app);
SetupHttpsSupport(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ private static IUrlHelper CreateMockUrlHelper(string slug = null)
private static IServiceProvider CreateServices(IUrlHelper urlHelper)
{
var services = new ServiceCollection();
var mockAccessor = new Mock<IHttpContextAccessor>();
mockAccessor.SetupGet(m => m.HttpContext).Returns(new DefaultHttpContext());

services.AddScoped(sp => urlHelper);
services.AddSingleton<IAvatarUrlService, DispatchAvatarUrlService>();
services.AddSingleton(mockAccessor.Object);
services.AddScoped<IUserAvatarUrlGenerator<DefaultAvatar>, DefaultAvatarUrlGenerator>();
services.AddScoped<IUserAvatarUrlGenerator<StorageFileAvatar>, StorageFileAvatarUrlGenerator>();
services.AddScoped<IUserAvatarUrlGenerator<GravatarAvatar>, GravatarAvatarUrlGenerator>();
Expand Down

0 comments on commit 1cb0998

Please sign in to comment.