public override async Task <AuthenticationState> GetAuthenticationStateAsync() { var access_token = await _tokenProvider.GetAcccessToken(); if (string.IsNullOrWhiteSpace(access_token)) { return(NotSignedIn()); } var identity = new ClaimsIdentity(claims: ParseTokenClaims(access_token), authenticationType: "Bearer", nameType: "name", roleType: "role"); return(new AuthenticationState(new ClaimsPrincipal(identity))); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Headers.Authorization is null && RuntimeInformation.ProcessArchitecture != Architecture.Wasm) { var access_token = await _tokenProvider.GetAcccessToken(); if (access_token is not null) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", access_token); } } var response = await base.SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode && response.Content.Headers.ContentType?.MediaType == "application/json") { if (response.Headers.TryGetValues("Request-ID", out IEnumerable <string>?values) && values is not null && values.Any()) { RestExceptionPayload restError = await response.Content.ReadFromJsonAsync(TodoTemplateJsonContext.Default.RestExceptionPayload); Type exceptionType = typeof(RestExceptionPayload).Assembly.GetType(restError.ExceptionType) ?? typeof(UnknownException); Exception exp = (Exception)Activator.CreateInstance(exceptionType, args: new object[] { restError.Message }); if (exp is ResourceValidationException resValidationException) { resValidationException.Details = restError.Details; } throw exp; } } response.EnsureSuccessStatusCode(); return(response); }