-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add authentication header to HTTP requests created by BrowserHttpSink? #7
Comments
Hi, did you ever find a solution for the http message handler? I am using blazor WASM standalone and windows authentication on my web api and need to attach the following to all my http requests. The log http request isnt working because I need to attach the same request. I tried adding "messageHandler: new AuthorizationMessageHandler()" to the logger config but it doesnt seem to have any affect. public class AuthorizationMessageHandler : DelegatingHandler |
Just checking if there is any update with this question. I'm in the same boat trying to figure out how to properly instantiate the AuthorizationMessageHandler. I've tried this, but it doesn't seem to work: var loggerHandler = builder.Services.BuildServiceProvider().GetRequiredService<AuthorizationMessageHandler>()
.ConfigureHandler(
authorizedUrls: new[] { builder.Configuration.GetValue<string>("ApiBaseUrl") },
scopes: new[] { "dadapi" });
var levelSwitch = new LoggingLevelSwitch();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
.WriteTo.BrowserHttp(endpointUrl: $"{builder.Configuration.GetValue<string>("ApiBaseUrl")}/ingest"
, controlLevelSwitch: levelSwitch
, messageHandler: loggerHandler)
.WriteTo.BrowserConsole()
.CreateLogger(); I'm not getting any errors, but the actual http call is never getting sent to the server. I get nothing when I try to monitor it in Fiddler. If I don't pass in the MessageHandler, then the call is made. Any help would be greatly appreciated. |
I would like logs generated for an authenticated user to be sent to the server along with the user's token in the headers.
If the user signs off, log entries should be sent to the server without this token.
Scenario:
I am using Serilog.Sinks.BrowserHttp in my Blazor WASM app:
My app uses some king
MyCustomJwtAuthService
(which is also used in my custom AuthenticationStateProvider implementation) which holds JWT token for currently signed in user (it also silently refreshes token). This JWT token is added to headers (_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
) - so whenHttpClient
is used then HTTP requests are authenticated.As I can see in sources Blazor registers in DI singleton for
HttpClient
, butBrowserHttpSink
creates its ownHttpClient
instance and Authorization header added byMyCustomJwtAuthService
does not affectHttpClient
instance created inBrowserHttpSink
- so I can not extract authenticated user data from requests perfomed byBrowserHttpSink
...I know that
BrowserHttpSink
acceptsHttpMessageHandler
as parameter but there are probably some issues related to used in BlazorWasmHttpMessageHandler
(eg. see how folks are creating instances here).Maybe it would be better for
BrowserHttpSink
to take a parameter of typeFunc<HttpClient>
- so that I could somehow decide whether to provide an instance created by DI or maybe a temporary instance before the DI system produces the correct instance for me?Although this may also not be ideal, because logs are sent to the server with a delay, so logs generated for an authenticated user can be sent after logging out ...
Any suggestions?
The text was updated successfully, but these errors were encountered: