Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ff8d81d0f | |||
| 62c8d8a687 | |||
| 6f1cdfe125 | |||
| dedcf0231c |
@@ -0,0 +1,27 @@
|
|||||||
|
using System.Net.Http.Headers;
|
||||||
|
using NexusReader.Application.Abstractions.Services;
|
||||||
|
|
||||||
|
namespace NexusReader.Web.Client.Handlers;
|
||||||
|
|
||||||
|
public class AuthenticationHeaderHandler : DelegatingHandler
|
||||||
|
{
|
||||||
|
private readonly INativeStorageService _storageService;
|
||||||
|
private const string TokenKey = "nexus_auth_token";
|
||||||
|
|
||||||
|
public AuthenticationHeaderHandler(INativeStorageService storageService)
|
||||||
|
{
|
||||||
|
_storageService = storageService;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var tokenResult = await _storageService.GetSecureString(TokenKey);
|
||||||
|
|
||||||
|
if (tokenResult.IsSuccess && !string.IsNullOrEmpty(tokenResult.Value))
|
||||||
|
{
|
||||||
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await base.SendAsync(request, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MediatR" Version="12.1.1" />
|
<PackageReference Include="MediatR" Version="12.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.7" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.7" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.7" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -33,7 +33,14 @@ builder.Services.AddCascadingAuthenticationState();
|
|||||||
|
|
||||||
// AI & Content Services
|
// AI & Content Services
|
||||||
builder.Services.AddScoped<IKnowledgeService, WasmKnowledgeService>();
|
builder.Services.AddScoped<IKnowledgeService, WasmKnowledgeService>();
|
||||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
|
||||||
|
builder.Services.AddTransient<NexusReader.Web.Client.Handlers.AuthenticationHeaderHandler>();
|
||||||
|
builder.Services.AddHttpClient("NexusAPI", client =>
|
||||||
|
{
|
||||||
|
client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
|
||||||
|
}).AddHttpMessageHandler<NexusReader.Web.Client.Handlers.AuthenticationHeaderHandler>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("NexusAPI"));
|
||||||
|
|
||||||
// Dummy registrations for server-only handlers to satisfy DI validation
|
// Dummy registrations for server-only handlers to satisfy DI validation
|
||||||
builder.Services.AddSingleton<IDbContextFactory<AppDbContext>>(new ThrowingDbContextFactory());
|
builder.Services.AddSingleton<IDbContextFactory<AppDbContext>>(new ThrowingDbContextFactory());
|
||||||
|
|||||||
@@ -96,6 +96,18 @@ builder.Services.ConfigureApplicationCookie(options =>
|
|||||||
options.Cookie.HttpOnly = true;
|
options.Cookie.HttpOnly = true;
|
||||||
options.ExpireTimeSpan = TimeSpan.FromDays(30);
|
options.ExpireTimeSpan = TimeSpan.FromDays(30);
|
||||||
options.SlidingExpiration = true;
|
options.SlidingExpiration = true;
|
||||||
|
options.Events.OnRedirectToLogin = context =>
|
||||||
|
{
|
||||||
|
if (context.Request.Path.StartsWithSegments("/api"))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 401;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.Response.Redirect(context.RedirectUri);
|
||||||
|
}
|
||||||
|
return Task.CompletedTask;
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.Configure<IdentityOptions>(options =>
|
builder.Services.Configure<IdentityOptions>(options =>
|
||||||
|
|||||||
Reference in New Issue
Block a user