Fix
This commit is contained in:
parent
68e6fa2353
commit
4714d25724
6 changed files with 86 additions and 20 deletions
|
|
@ -9,6 +9,7 @@ using System.Text;
|
|||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using GsDChat.Client.State;
|
||||
using GsDChat.Core.Models;
|
||||
using GsDChat.Core.Protocol;
|
||||
|
|
@ -42,6 +43,30 @@ public class ChatClientConnection : IDisposable
|
|||
};
|
||||
}
|
||||
|
||||
private async Task<string> FetchCsrfTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = await _httpClient.GetAsync("/api/csrf-token");
|
||||
if (res.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await res.Content.ReadAsStringAsync();
|
||||
var tokenData = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (tokenData != null && tokenData.TryGetValue("token", out var token))
|
||||
{
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_state.SetStatus($"Failed to fetch CSRF token: {ex.Message}");
|
||||
}
|
||||
|
||||
// Fallback to empty string if CSRF token fetch fails
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task<ServerInfoResponse?> GetServerInfoAsync()
|
||||
{
|
||||
try
|
||||
|
|
@ -104,13 +129,15 @@ public class ChatClientConnection : IDisposable
|
|||
|
||||
try
|
||||
{
|
||||
// Fetch CSRF token from server
|
||||
string csrfToken = await FetchCsrfTokenAsync();
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "/api/auth/register")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
// Add CSRF token
|
||||
var csrfToken = Guid.NewGuid().ToString("N");
|
||||
request.Headers.Add("X-CSRF-Token", csrfToken);
|
||||
|
||||
var res = await _httpClient.SendAsync(request);
|
||||
|
|
@ -160,13 +187,15 @@ public class ChatClientConnection : IDisposable
|
|||
|
||||
try
|
||||
{
|
||||
// Fetch CSRF token from server
|
||||
string csrfToken = await FetchCsrfTokenAsync();
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "/api/auth/login")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
// Add CSRF token
|
||||
var csrfToken = Guid.NewGuid().ToString("N");
|
||||
request.Headers.Add("X-CSRF-Token", csrfToken);
|
||||
|
||||
var res = await _httpClient.SendAsync(request);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue