private void InterceptResponse(object sender, HttpClientInterceptorEventArgs e) { string message = string.Empty; if (!e.Response.IsSuccessStatusCode) { var statusCode = e.Response.StatusCode; switch (statusCode) { case HttpStatusCode.NotFound: _navManager.NavigateTo("/404"); message = "The requested resorce was not found."; break; case HttpStatusCode.Unauthorized: _navManager.NavigateTo("/unauthorized"); message = "User is not authorized"; break; default: _navManager.NavigateTo("/500"); message = "Something went wrong, please contact Administrator"; break; } throw new HttpResponseException(message); } }
private void InterceptResponse(object sender, HttpClientInterceptorEventArgs e) { string message = string.Empty; if (e.Response.IsSuccessStatusCode == false) { var responseCode = e.Response.StatusCode; switch (responseCode) { case HttpStatusCode.NotFound: _navManager.NavigateTo("/404"); message = "The requested resource was not found."; break; case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: _navManager.NavigateTo("/unauthorized"); message = "You are not authorized to access this resource."; break; default: _navManager.NavigateTo("/500"); message = "Something went wrong, please contact the administrator."; break; } } // throw new HttpRequestException(message); }
public async Task InterceptBeforeHttpAsync(object sender, HttpClientInterceptorEventArgs e) { var absPath = e.Request.RequestUri.AbsolutePath; if (!absPath.Contains("token") && !absPath.Contains("accounts")) { try { var token = await _authenticationManager.TryRefreshToken(); if (!string.IsNullOrEmpty(token)) { _snackBar.Add(_localizer["Refreshed Token."], Severity.Success); e.Request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); } } catch (Exception ex) { Console.WriteLine(ex.Message); _snackBar.Add(_localizer["You are Logged Out."], Severity.Error); await _authenticationManager.Logout(); _navigationManager.NavigateTo("/"); } } }
private async Task HttpClientInterceptor_AfterSendAsync(object sender, HttpClientInterceptorEventArgs e) { Console.WriteLine("HttpClientInterceptor.AfterSendAsync-1"); await Task.Delay(3000); Console.WriteLine("HttpClientInterceptor.AfterSendAsync-2"); }
private void HandleResponse(object sender, HttpClientInterceptorEventArgs e) { if (e.Response == null) { _navManager.NavigateTo("/error"); throw new HttpResponseException("Server not available"); } var message = ""; if (!e.Response.IsSuccessStatusCode) { switch (e.Response.StatusCode) { case HttpStatusCode.NotFound: _navManager.NavigateTo("/404"); message = "Resource not found."; break; case HttpStatusCode.Unauthorized: _navManager.NavigateTo("/unauthorized"); message = "Unauthorized access"; break; default: _navManager.NavigateTo("/error"); message = "Something went wrong. Please contact the administrator."; break; } throw new HttpResponseException(message); } }
private async Task HttpClientInterceptor_BeforeSendAsync(object sender, HttpClientInterceptorEventArgs e) { Console.WriteLine("HttpClientInterceptor.BeforeSendAsync-1"); var sampleTextB = await this.HttpClient.GetStringAsync("content/sample-text-B.txt"); Console.WriteLine($"Sample Text B: {sampleTextB}"); Console.WriteLine("HttpClientInterceptor.BeforeSendAsync-2"); }
public async Task InterceptAfterHttpAsync(object sender, HttpClientInterceptorEventArgs e) { if (e.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { await _authService.Logout(); } else if (e.Response.StatusCode == System.Net.HttpStatusCode.ExpectationFailed) { await _authService.ClearLocalStorage(); await _jsRuntime.InvokeVoidAsync("showModalUnauthorized"); } }
public async Task InterceptBeforeHttpAsync(object sender, HttpClientInterceptorEventArgs e) { var absPath = e.Request.RequestUri.AbsolutePath; if (!absPath.Contains("Usuario")) { var token = await _refreshTokenService.TryRefreshToken(); if (!string.IsNullOrEmpty(token)) { e.Request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token); } } }
private async Task InterceptBeforeSendAsync(object sender, HttpClientInterceptorEventArgs e) { var absolutePath = e.Request.RequestUri.AbsolutePath; //does the path contain the token or account info. . If not then refresh token. if (!absolutePath.Contains("token") && !absolutePath.Contains("account")) { var token = await _refreshTokenService.TryRefreshToken(); if (!string.IsNullOrEmpty(token)) { e.Request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token); } } }
public async Task InterceptBeforeHttpAsync(object sender, HttpClientInterceptorEventArgs e) { var absolutePath = e.Request.RequestUri.AbsolutePath; e.Request.Headers.TryAddWithoutValidation("Access-Control-Allow-Origin", "*"); if (!absolutePath.Contains("identity/") || absolutePath.Contains("identity/logout") || absolutePath.Contains("identity/registration")) { var token = await _tokenService.TryRefreshTokenAsync <LoginModel>(); if (!string.IsNullOrWhiteSpace(token)) { e.Request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token); } } }
private static async Task OnAfterSendAsync(object sender, HttpClientInterceptorEventArgs args) { Console.WriteLine("AfterSend event of HttpClientInterceptor"); Console.WriteLine($" - {args.Request.Method} {args.Request.RequestUri}"); Console.WriteLine($" - HTTP Status {args.Response?.StatusCode}"); var capturedContent = await args.GetCapturedContentAsync(); Console.WriteLine($" - Content Headers"); foreach (var headerText in capturedContent.Headers.Select(h => $"{h.Key}: {string.Join(", ", h.Value)}")) { Console.WriteLine($" - {headerText}"); } var httpContentString = await capturedContent.ReadAsStringAsync(); Console.WriteLine($" - HTTP Content \"{httpContentString}\""); }
private void HandleResponse(object sender, HttpClientInterceptorEventArgs e) { if (e.Response == null) { _navManager.NavigateTo("/error"); throw new HttpResponseException("Server Not Available"); } var message = ""; //Message switch case for custom messages if (!e.Response.IsSuccessStatusCode) { //Switch case for selecting the correct error to display. //default to 404 if none are matched before switch (e.Response.StatusCode) { case HttpStatusCode.NotFound: _navManager.NavigateTo("/404"); message = "Resource not found."; break; case HttpStatusCode.BadRequest: message = "Invalid request. Please try again."; _toastService.ShowError(message); break; case HttpStatusCode.Unauthorized: _navManager.NavigateTo("/unauthorized"); message = "Unauthorized access"; break; default: _navManager.NavigateTo("/404"); message = "Something went wrong :( unlucky. Please contact the administrator."; break; } throw new HttpResponseException(message); } }
private void Interceptor_AfterSend(object sender, HttpClientInterceptorEventArgs e) { if (e.Response is null) { navigationManager.NavigateTo("/error"); throw new HttpResponseException("Server not available"); } var message = ""; if (!e.Response.IsSuccessStatusCode) { switch (e.Response.StatusCode) { case System.Net.HttpStatusCode.Unauthorized: navigationManager.NavigateTo("/404"); message = "Resource not found"; break; case System.Net.HttpStatusCode.NotFound: navigationManager.NavigateTo("/unauthorized"); message = "Unauthorized access"; break; case System.Net.HttpStatusCode.BadRequest: message = "Invalid request. Please try again"; toastService.ShowError(message); break; default: navigationManager.NavigateTo("/error"); message = "Something was wrong. Please contact the administrator"; break; } throw new HttpResponseException(message); } }
private void HandleErrors(object s, HttpClientInterceptorEventArgs e) { if (e.Response?.IsSuccessStatusCode == true) { return; } if (e.Response == null) { AddErrorAlert(NotReachableError); return; } switch (e.Response.StatusCode) { case HttpStatusCode.BadRequest: var errorHandler = ""; foreach (var httpResponseHeader in e.Response.Headers) { if (httpResponseHeader.Key.EndsWith(HttpErrorHeader)) { errorHandler = e.Response.Headers.GetValues(httpResponseHeader.Key).FirstOrDefault(); } } if (!string.IsNullOrEmpty(errorHandler)) { AddErrorAlert(errorHandler); } break; case HttpStatusCode.NotFound: AddErrorAlert(NotFoundError); break; default: AddErrorAlert(e.Response.Content.ToString()); break; } }
private static void OnBeforeSend(object sender, HttpClientInterceptorEventArgs args) { Console.WriteLine("BeforeSend event of HttpClientInterceptor"); Console.WriteLine($" - {args.Request.Method} {args.Request.RequestUri}"); }
private static void OnAfterSend(object sender, HttpClientInterceptorEventArgs args) { Console.WriteLine("AfterSend event of HttpClientInterceptor"); Console.WriteLine($" - {args.Request.Method} {args.Request.RequestUri}"); Console.WriteLine($" - HTTP Status {args.Response?.StatusCode}"); }
private void HttpClientInterceptor_AfterSend(object sender, HttpClientInterceptorEventArgs e) { Console.WriteLine("HttpClientInterceptor.AfterSend"); }