Setup for hosting behind Proxy.

This commit is contained in:
Steven Hildreth 2019-01-11 18:27:49 -06:00
parent d7ce79b031
commit cde56ae735
9 changed files with 49 additions and 21 deletions

View file

@ -35,6 +35,7 @@ namespace Roadie.Library.Configuration
ImageSize ThumbnailImageSize { get; set; }
Dictionary<string, string> TrackPathReplace { get; set; }
bool UseSSLBehindProxy { get; set; }
string BehindProxyHost { get; set; }
string WebsocketAddress { get; set; }
}
}

View file

@ -50,6 +50,7 @@ namespace Roadie.Library.Configuration
public ImageSize ThumbnailImageSize { get; set; }
public Dictionary<string, string> TrackPathReplace { get; set; }
public bool UseSSLBehindProxy { get; set; }
public string BehindProxyHost { get; set; }
public string WebsocketAddress { get; set; }
public RoadieSettings()

View file

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Roadie.Library.Configuration;
using Roadie.Library.Utility;
namespace Roadie.Api.Services
@ -8,9 +9,19 @@ namespace Roadie.Api.Services
public string BaseUrl { get; set; }
public string ImageBaseUrl { get; set; }
public HttpContext(IUrlHelper urlHelper)
public HttpContext(IRoadieSettings configuration, IUrlHelper urlHelper)
{
this.BaseUrl = $"{ urlHelper.ActionContext.HttpContext.Request.Scheme}://{ urlHelper.ActionContext.HttpContext.Request.Host }";
var scheme = urlHelper.ActionContext.HttpContext.Request.Scheme;
if (configuration.UseSSLBehindProxy)
{
scheme = "https";
}
var host = urlHelper.ActionContext.HttpContext.Request.Host;
if(!string.IsNullOrEmpty(configuration.BehindProxyHost))
{
host = new Microsoft.AspNetCore.Http.HostString(configuration.BehindProxyHost);
}
this.BaseUrl = $"{ scheme }://{ host }";
this.ImageBaseUrl = $"{ this.BaseUrl}/images";
}
}

View file

@ -9,7 +9,7 @@
<PackageReference Include="Hashids.net" Version="1.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9.2" />
</ItemGroup>
<ItemGroup>

View file

@ -17,6 +17,7 @@ using System.Net.Http;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebUtilities;
using Roadie.Library.Utility;
namespace Roadie.Api.Controllers
{
@ -35,6 +36,7 @@ namespace Roadie.Api.Controllers
private ICacheManager CacheManager { get; }
private IAdminService AdminService { get; }
private IEmailSender EmailSender { get; }
private IHttpContext RoadieHttpContext { get; }
public AccountController(
IAdminService adminService,
@ -44,7 +46,8 @@ namespace Roadie.Api.Controllers
ILogger<AccountController> logger,
ITokenService tokenService,
ICacheManager cacheManager,
IEmailSender emailSender)
IEmailSender emailSender,
IHttpContext httpContext)
{
this.UserManager = userManager;
this.SignInManager = signInManager;
@ -57,6 +60,7 @@ namespace Roadie.Api.Controllers
configuration.GetSection("RoadieSettings").Bind(this.RoadieSettings);
this.AdminService = adminService;
this.EmailSender = emailSender;
this.RoadieHttpContext = httpContext;
}
[HttpPost]
@ -88,7 +92,7 @@ namespace Roadie.Api.Controllers
await this.EmailSender.SendEmailAsync(user.Email, $"Confirm your { this.RoadieSettings.SiteName } email", $"Please confirm your { this.RoadieSettings.SiteName } account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
}
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
var avatarUrl = $"{ this.RoadieHttpContext.ImageBaseUrl }/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
return Ok(new
{
Username = user.UserName,
@ -159,7 +163,7 @@ namespace Roadie.Api.Controllers
var t = await this.TokenService.GenerateToken(user, this.UserManager);
this.Logger.LogInformation($"Successfully created and authenticated User [{ registerModel.Username}]");
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
var avatarUrl = $"{ this.RoadieHttpContext.ImageBaseUrl }/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
return Ok(new
{
Username = user.UserName,
@ -226,7 +230,7 @@ namespace Roadie.Api.Controllers
{
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
await SignInManager.SignInAsync(user, isPersistent: false);
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
var avatarUrl = $"{ this.RoadieHttpContext.ImageBaseUrl }/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
var t = await this.TokenService.GenerateToken(user, this.UserManager);
return Ok(new
{

View file

@ -8,6 +8,7 @@ using Roadie.Library.Caching;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
using System;
using System.Net;
using System.Threading.Tasks;
@ -23,13 +24,15 @@ namespace Roadie.Api.Controllers
{
private readonly ITokenService TokenService;
private IUserService UserService { get; }
private IHttpContext RoadieHttpContext { get; }
public UserController(IUserService userService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, ITokenService tokenService, UserManager<ApplicationUser> userManager)
public UserController(IUserService userService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, ITokenService tokenService, UserManager<ApplicationUser> userManager, IHttpContext httpContext)
: base(cacheManager, configuration, userManager)
{
this.Logger = logger.CreateLogger("RoadieApi.Controllers.UserController");
this.UserService = userService;
this.TokenService = tokenService;
this.RoadieHttpContext = httpContext;
}
[HttpGet("{id}")]
@ -283,7 +286,7 @@ namespace Roadie.Api.Controllers
var modelUser = await UserManager.FindByNameAsync(model.UserName);
var t = await this.TokenService.GenerateToken(modelUser, this.UserManager);
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ modelUser.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
var avatarUrl = $"{ this.RoadieHttpContext.ImageBaseUrl }/user/{ modelUser.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
return Ok(new
{
IsSuccess = true,

View file

@ -34,7 +34,7 @@
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFileAlternate" Version="2.0.9" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9.2" />
</ItemGroup>
<ItemGroup>

View file

@ -33,11 +33,15 @@ namespace Roadie.Api
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
private ILogger Logger { get; }
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
this._configuration = configuration;
this._loggerFactory = loggerFactory;
this.Logger = this._loggerFactory.CreateLogger<Startup>();
TypeAdapterConfig<Roadie.Library.Data.Image, Roadie.Library.Models.Image>
.NewConfig()
.Map(i => i.ArtistId,
@ -80,15 +84,6 @@ namespace Roadie.Api
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("CORSPolicy", builder =>
{
builder
.WithOrigins("http://localhost:8080", "https://localhost:8080", "http://localhost:80", "https://localhost:80")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}));
services.AddSingleton<ITokenService, TokenService>();
services.AddSingleton<IHttpEncoder, HttpEncoder>();
services.AddSingleton<IEmailSender, EmailSenderService>();
@ -124,6 +119,17 @@ namespace Roadie.Api
});
services.Configure<IConfiguration>(this._configuration);
var corsOrigins = (this._configuration["CORSOrigins"] ?? "http://localhost:8080").Split('|');
this.Logger.LogDebug("Setting Up CORS Policy [{0}]", string.Join(", ", corsOrigins));
services.AddCors(options => options.AddPolicy("CORSPolicy", builder =>
{
builder
.WithOrigins(corsOrigins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}));
services.AddSingleton<IRoadieSettings, RoadieSettings>(ctx =>
{
@ -247,7 +253,8 @@ namespace Roadie.Api
{
var actionContext = factory.GetService<IActionContextAccessor>()
.ActionContext;
return new HttpContext(new UrlHelper(actionContext));
return new HttpContext(factory.GetService<IRoadieSettings>(), new UrlHelper(actionContext));
});
}

View file

@ -13,7 +13,7 @@
"Override": {
"System": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
"Microsoft.EntityFrameworkCore": "Information"
}
},
"WriteTo": [
@ -50,6 +50,7 @@
"ConnectionStrings": {
"RoadieDatabaseConnection": "server=voyager;userid=roadie;password=MenAtW0rk668;persistsecurityinfo=True;database=roadie;ConvertZeroDateTime=true"
},
"CORSOrigins": "http://localhost:8080|https://localhost:8080|http://localhost:80|https://localhost:80",
"RoadieSettings": {
"SiteName": "Roadie",
"DefaultTimeZone": "US/Central",