Last active
January 23, 2026 19:33
-
-
Save pedroinfo/4bbf6a9be8dcc622bcd71ae7fbe7178f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "Logging": { | |
| "LogLevel": { | |
| "Default": "Information", | |
| "Microsoft.AspNetCore": "Warning", | |
| "Yarp": "Debug" | |
| } | |
| }, | |
| "ReverseProxy": { | |
| "Routes": { | |
| "blazor-route": { | |
| "ClusterId": "blazor-cluster", | |
| "Match": { | |
| "Path": "{**catch-all}" | |
| }, | |
| "Transforms": [ | |
| { | |
| "RequestHeader": "X-Forwarded-Host", | |
| "Set": "{Host}" | |
| }, | |
| { | |
| "RequestHeader": "X-Forwarded-Proto", | |
| "Set": "{Scheme}" | |
| }, | |
| { | |
| "RequestHeader": "X-Forwarded-Prefix", | |
| "Set": "/" | |
| } | |
| ] | |
| } | |
| }, | |
| "Clusters": { | |
| "blazor-cluster": { | |
| "Destinations": { | |
| "blazor-server": { | |
| "Address": "http://localhost:5000/" // SUA PORTA DO BLAZOR | |
| } | |
| }, | |
| "SessionAffinity": { | |
| "Enabled": true, | |
| "Policy": "Cookie", | |
| "AffinityKeyName": ".AspNetCore.Affinity" | |
| }, | |
| "HttpClient": { | |
| "WebSocketClient": { | |
| "KeepAliveInterval": "00:02:00" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var builder = WebApplication.CreateBuilder(args); | |
| // Configuração do YARP que preserva cookies e headers | |
| builder.Services.AddReverseProxy() | |
| .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")) | |
| .AddTransforms(builderContext => | |
| { | |
| // Preserva todos os headers importantes | |
| builderContext.AddRequestTransform(async context => | |
| { | |
| // Copia todos os headers da requisição original | |
| foreach (var header in context.HttpContext.Request.Headers) | |
| { | |
| if (!context.ProxyRequest.Headers.Contains(header.Key)) | |
| { | |
| context.ProxyRequest.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); | |
| } | |
| } | |
| // Especificamente para cookies (importante para autenticação) | |
| var cookieHeader = context.HttpContext.Request.Headers["Cookie"]; | |
| if (!string.IsNullOrEmpty(cookieHeader)) | |
| { | |
| context.ProxyRequest.Headers.Remove("Cookie"); | |
| context.ProxyRequest.Headers.Add("Cookie", cookieHeader); | |
| } | |
| // Headers de autorização (Bearer tokens) | |
| var authHeader = context.HttpContext.Request.Headers["Authorization"]; | |
| if (!string.IsNullOrEmpty(authHeader)) | |
| { | |
| context.ProxyRequest.Headers.Remove("Authorization"); | |
| context.ProxyRequest.Headers.Add("Authorization", authHeader); | |
| } | |
| }); | |
| }); | |
| var app = builder.Build(); | |
| app.UseWebSockets(); | |
| app.MapReverseProxy(); | |
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| builder.Services.AddReverseProxy() | |
| .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")) | |
| .AddTransforms(builderContext => | |
| { | |
| // Adicionar transform personalizada | |
| builderContext.AddRequestTransform(async transformContext => | |
| { | |
| // Copiar todos os headers | |
| foreach (var header in transformContext.HttpContext.Request.Headers) | |
| { | |
| if (!transformContext.ProxyRequest.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray())) | |
| { | |
| transformContext.ProxyRequest.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); | |
| } | |
| } | |
| // Copiar cookies | |
| var cookies = transformContext.HttpContext.Request.Cookies; | |
| if (cookies.Count > 0) | |
| { | |
| var cookieHeader = string.Join("; ", cookies.Select(c => $"{c.Key}={c.Value}")); | |
| if (!string.IsNullOrEmpty(cookieHeader)) | |
| { | |
| transformContext.ProxyRequest.Headers.Remove("Cookie"); | |
| transformContext.ProxyRequest.Headers.TryAddWithoutValidation("Cookie", cookieHeader); | |
| } | |
| } | |
| // Headers de proxy padrão | |
| transformContext.ProxyRequest.Headers.TryAddWithoutValidation("X-Forwarded-Host", transformContext.HttpContext.Request.Host.Value); | |
| transformContext.ProxyRequest.Headers.TryAddWithoutValidation("X-Forwarded-Proto", transformContext.HttpContext.Request.Scheme); | |
| transformContext.ProxyRequest.Headers.TryAddWithoutValidation("X-Forwarded-For", transformContext.HttpContext.Connection.RemoteIpAddress?.ToString()); | |
| }); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Optional: in DEV, accept self-signed certs
builder.Services.AddReverseProxy()
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
});