问题出现的原因是由于在使用ASP .NET Blazor Server时,可能会遇到使用nginx作为反向代理的情况,从而导致Auth和重定向问题。为了解决这个问题,我们需要以下步骤:
location /blazorserver {
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwardedHeaders();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
app.Use(async (context, next) =>
{
context.Request.Scheme = "https";
await next.Invoke();
});
这些步骤将解决这个问题,使您的Blazor Server应用程序在nginx反向代理下正常工作。