-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Hello,
I have created project in Blazor Web App .Net 8 Render Interactive Auto.
I am authorizing the app via idp
so sequence of process goes like this:
- visit client app
- Redirect to idp provider
- Successfully login created access token and claims
- Redirect to client app with access token
- DelegatingHandler as AuthorizationHandler in Server Side. It used to add Access token into data Header to access the secured api controllers. So I use httpContextAccessor (HttpContext)
- here it throwing the errro HttpContext is null.
- in browser console showing WebSocket has issue.
and this issue I only facing in production. In development it works fine.
I refer the authorization from
https://github.com/mohaaron/CustomAuthorizationHandler
BlazorWebApp/Services/ServerTokenHandler.cs this is the path . I named AuthorizationHandler.
Here is my code, please check-
`public class AuthorizationHandler(IHttpContextAccessor httpContextAccessor) : DelegatingHandler
{
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var httpContext = _httpContextAccessor.HttpContext ??
throw new InvalidOperationException("No HttpContext available from the IHttpContextAccessor!");
if (httpContext != null && httpContext.Request.Cookies.TryGetValue("access_token", out var token))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
return await base.SendAsync(request, cancellationToken);
} }
services.AddTransient<AuthorizationHandler>();
var httpClientBuilder = services.AddHttpClient("ApiClient", client =>
{ client.BaseAddress = new Uri("https://localhost:7067/"); }).AddHttpMessageHandler<AuthorizationHandler>();
`
Please help me upon this, this is really exhausting when it working in development and not in production.