Ensure CORS response header values are valid
A cross-origin resource sharing (CORS) request was blocked because of invalid or missing response headers of the request or the associated preflight request.
To fix this issue, ensure the response to the CORS request and/or the associated preflight request are not missing headers and use valid header values.
Note that if an opaque response is sufficient, the request's mode can be set to no-cors to fetch the resource with CORS disabled; that way CORS headers are not required but the response content is inaccessible (opaque).
1 solicitação
Solicitação Status Solicitação de Simulação (se problemático) Cabeçalho Problema Valor Inválido (se disponível)
The error you're encountering occurs because your Blazor application is running inside a WebView (provided by Tauri), which enforces the same security restrictions as a regular web browser. When you make an HTTP request from your Blazor application, it's effectively making that request from the browser context. This means it's subject to Cross-Origin Resource Sharing (CORS) policies.
In your case, when your Blazor app attempts to make a request to https://hil-speed.hetzner.com, the server does not include the necessary Access-Control-Allow-Origin header in its response. As a result, the browser (WebView) blocks the request due to CORS policy violations.
Why is this happening?
CORS Restrictions: Browsers enforce CORS to prevent web pages from making requests to a different domain than the one that served the web page, unless the server explicitly allows it.
WebView Context: Even though Tauri is a desktop application framework, it uses a WebView to render the frontend. This means frontend code (like your Blazor application) runs in a browser-like environment with the same security restrictions.
How to Fix It:
To resolve this issue, you need to move the HTTP request from the frontend (Blazor/WebView) to the backend (Tauri's Rust or .NET backend). Requests made from the backend are not subject to CORS restrictions because they are made from the user's machine directly, not from a sandboxed browser environment.
Here's how you can do it:
1. Use Tauri Commands to Call Backend Functions
Tauri allows you to define backend functions (commands) that can be called from the frontend. You can move your HTTP request logic to a backend command.
Steps:
a. Define a Backend Command: In your Tauri application, define a command that performs the HTTP request.
For a Tauri app using C#, you can define a command in your Program.cs or wherever you initialize Tauri:
csharp
using Tauri.Net;
public class TauriApi
{
[TauriCommand]
public async Task<bool> CheckServerAvailabilityAsync()
{
try
{
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(5);
var response = await httpClient.GetAsync("https://hil-speed.hetzner.com");
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
}
b. Register the Command: Ensure that the command is registered in your Tauri app.
While it's technically possible to adjust Tauri's security settings to relax CORS policies, it's not recommended because it exposes your application to security risks.
3. Alternative: Use Tauri's HTTP API
Tauri provides an HTTP API that can make HTTP requests from the backend. However, using commands, as described above, is a better fit in this context because it keeps your backend logic organized and accessible.
Summary
By moving your HTTP request to the backend, you avoid CORS restrictions because the request is no longer made from the browser context. Instead, it's made from the user's machine directly, which has no such limitations.
Note: Ensure that your frontend only calls backend commands and doesn't handle sensitive data directly. This aligns with best practices for security in desktop applications.