Path: redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Services/ModuleClient.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Services/ModuleClient.cs ---- using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using Microsoft.AspNetCore.Components.Authorization; using RedbIdentityApp.Contracts; using RedbIdentityApp.Web.Auth; namespace RedbIdentityApp.Web.Services; /// /// The module's API, called with the signed-in user's access token. The HttpClient comes configured from /// Program.cs (the module's address). What the module answers becomes something a page can show: /// /// 401: the token is no longer good (revoked at Identity): the session ends, /// ; /// 403: the user lacks the right (); /// 503: Identity is down (). /// /// The user is taken from the circuit's authentication state, which the client follows through /// from its creation until the circuit's scope /// disposes it, so a call never goes out with a state that has changed since. /// public sealed class ModuleClient : IDisposable { private readonly HttpClient http; private readonly AuthenticationStateProvider authentication; private readonly AccessTokens tokens; private readonly UserTokenStore store; private Task? state; public ModuleClient(HttpClient http, AuthenticationStateProvider authentication, AccessTokens tokens, UserTokenStore store) { this.http = http; this.authentication = authentication; this.tokens = tokens; this.store = store; authentication.AuthenticationStateChanged += OnAuthenticationStateChanged; } public void Dispose() => authentication.AuthenticationStateChanged -= OnAuthenticationStateChanged; private void OnAuthenticationStateChanged(Task changed) => state = changed; public Task MeAsync() => SendAsync(HttpMethod.Get, "api/me", null); public Task> NotesAsync() => SendAsync>(HttpMethod.Get, "api/notes", null); public Task AddNoteAsync(NoteDto note) => SendAsync(HttpMethod.Post, "api/notes", note); public Task DeleteNoteAsync(long id) => SendAsync(HttpMethod.Delete, $"api/notes/{id}", null); public Task> UsersAsync(int page) => SendAsync>(HttpMethod.Get, $"api/admin/users?page={page}", null); private async Task SendAsync(HttpMethod method, string uri, object? body) { var sessionKey = (await (state ??= authentication.GetAuthenticationStateAsync())).User.FindFirst(UserTokenStore.SessionClaim)?.Value ?? throw new SessionEndedException(); var accessToken = await tokens.GetAsync(sessionKey, CancellationToken.None); using var request = new HttpRequestMessage(method, uri); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); if (body is not null) request.Content = JsonContent.Create(body, body.GetType()); using var response = await http.SendAsync(request); switch (response.StatusCode) { case HttpStatusCode.Unauthorized: await store.RemoveAsync(sessionKey, CancellationToken.None); throw new SessionEndedException(); case HttpStatusCode.ServiceUnavailable: throw new IdentityUnavailableException(); case HttpStatusCode.NoContent: return default!; } if (response.IsSuccessStatusCode) return (await response.Content.ReadFromJsonAsync())!; var error = response.Content.Headers.ContentType?.MediaType == "application/json" ? await response.Content.ReadFromJsonAsync() : null; throw new ModuleException( error?.Message ?? $"The module answered {(int)response.StatusCode} {response.ReasonPhrase}.", response.StatusCode == HttpStatusCode.Forbidden); } } /// A module call that did not succeed; the message is meant for the user. public sealed class ModuleException(string message, bool forbidden) : Exception(message) { public bool Forbidden { get; } = forbidden; }