fix: add extension method for getting base url that checks for proxy headers
This commit is contained in:
@@ -34,7 +34,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseUrl = $"{HttpContextAccessor.HttpContext.Request.Scheme}://{HttpContextAccessor.HttpContext.Request.Host}";
|
BaseUrl = HttpContextAccessor.HttpContext.Request.GetBaseUrl();
|
||||||
OpenGraphUrl = $"{BaseUrl}{HttpContextAccessor.HttpContext.Request.Path}";
|
OpenGraphUrl = $"{BaseUrl}{HttpContextAccessor.HttpContext.Request.Path}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,6 @@
|
|||||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||||
@using Microsoft.JSInterop
|
@using Microsoft.JSInterop
|
||||||
@using Blog
|
@using Blog
|
||||||
|
@using Blog.Extensions
|
||||||
@using Blog.Components
|
@using Blog.Components
|
||||||
@using Blog.Components.Utils
|
@using Blog.Components.Utils
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Blog.Extensions;
|
||||||
|
public static class HttpRequestExtensions
|
||||||
|
{
|
||||||
|
public static string GetBaseUrl(this HttpRequest request)
|
||||||
|
{
|
||||||
|
var scheme = request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? request.Scheme;
|
||||||
|
var host = request.Headers["X-Forwarded-Host"].FirstOrDefault() ?? request.Host.Value;
|
||||||
|
return $"{scheme}://{host}";
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-6
@@ -1,3 +1,5 @@
|
|||||||
|
using Blog.Extensions;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.Services.ConfigureOptions<FilePostServiceOptionsSetup>();
|
builder.Services.ConfigureOptions<FilePostServiceOptionsSetup>();
|
||||||
@@ -25,12 +27,8 @@ app.UseStatusCodePagesWithRedirects("/Error/{0}");
|
|||||||
app
|
app
|
||||||
.MapGet("/rss", async (HttpContext context, IPostService postService) =>
|
.MapGet("/rss", async (HttpContext context, IPostService postService) =>
|
||||||
{
|
{
|
||||||
var req = context.Request;
|
var baseUrl = context.Request.GetBaseUrl();
|
||||||
|
var url = $"{baseUrl}{context.Request.PathBase}";
|
||||||
var scheme = req.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? req.Scheme;
|
|
||||||
var host = req.Headers["X-Forwarded-Host"].FirstOrDefault() ?? req.Host.Value;
|
|
||||||
|
|
||||||
var url = $"{scheme}://{host}{req.PathBase}";
|
|
||||||
|
|
||||||
var posts = await postService.GetPostsAsync();
|
var posts = await postService.GetPostsAsync();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user