gehgassi_app/gehGassiApp/gehGassiApp.Core/Helper/DevHttpsConnectionHelper.cs
Max Mannstein 1a18eea28a Update configuration, enhance UI, and add new features
This commit includes significant updates to the application, focusing on configuration changes, UI enhancements, and new features:

- Updated `appsettings.json` to change the database name and add a new connection string for encryption.
- Modified `launchSettings.json` to allow the application to listen on all network interfaces.
- Introduced new image assets and SVG icons to improve visual elements.
- Added `UserPreferencesService` to manage user acceptance of terms and conditions.
- Expanded `OnboardingViewModel` to manage onboarding steps and user interactions.
- Created new popup ViewModels for managing user agreements and subscription offers.
- Restructured various XAML files to improve layout consistency and data binding.
- Updated project files to reflect version changes and improve organization.
- Introduced a new `SubscriptionModel` class to manage subscription data in the UI.

Overall, these changes enhance the application's structure, usability, and responsiveness.
2025-06-19 21:20:21 +02:00

109 lines
3.6 KiB
C#

#nullable enable
using System.Net.Security;
namespace gehGassiApp.Core.Helper
{
/// <summary>
/// Hilfsklasse für das lokale Debugging.
/// Ermöglicht den Zugriff auf die lokal REST-API
/// </summary>
public class DevHttpsConnectionHelper
{
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="sslPort">Port</param>
public DevHttpsConnectionHelper(int sslPort)
{
SslPort = sslPort;
DevServerRootUrl = FormattableString.Invariant($"https://{DevServerName}:{SslPort}");
#if WINDOWS
LazyHttpClient = new Lazy<HttpClient>(() => new HttpClient());
#else
LazyHttpClient = new Lazy<HttpClient>(() => new HttpClient(GetPlatformMessageHandler()));
#endif
}
public int SslPort { get; }
public string DevServerName =>
#if WINDOWS
"localhost";
#elif ANDROID
"192.168.0.65";
#elif IOS || MACCATALYST
"192.168.0.65";
#else
throw new PlatformNotSupportedException("Only Windows and Android currently supported.");
#endif
public string DevServerRootUrl { get; }
private Lazy<HttpClient> LazyHttpClient;
public HttpClient HttpClient => LazyHttpClient.Value;
public HttpMessageHandler? GetPlatformMessageHandler()
{
#if WINDOWS
return null;
#elif IOS || MACCATALYST
var handler = new NSUrlSessionHandler
{
TrustOverrideForUrl = IsHttpsLocalhost
};
return handler;
#elif ANDROID
var handler = new CustomAndroidMessageHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == SslPolicyErrors.None;
};
return handler;
#else
throw new PlatformNotSupportedException("Only Windows and Android currently supported.");
#endif
}
#if ANDROID
internal sealed class CustomAndroidMessageHandler : Xamarin.Android.Net.AndroidMessageHandler
{
protected override Javax.Net.Ssl.IHostnameVerifier GetSSLHostnameVerifier(Javax.Net.Ssl.HttpsURLConnection connection)
=> new CustomHostnameVerifier();
private sealed class CustomHostnameVerifier : Java.Lang.Object, Javax.Net.Ssl.IHostnameVerifier
{
public bool Verify(string? hostname, Javax.Net.Ssl.ISSLSession? session)
{
return
Javax.Net.Ssl.HttpsURLConnection.DefaultHostnameVerifier.Verify(hostname, session)
|| hostname == "192.168.0.65" && session.PeerPrincipal?.Name == "CN=localhost";
}
}
}
#elif IOS || MACCATALYST
public bool IsHttpsLocalhost(NSUrlSessionHandler sender, string url, Security.SecTrust trust)
{
if (url.StartsWith("https://localhost") || url.StartsWith("https://192.168.0.65"))
return true;
return false;
}
#endif
}
public static class DevHttpsConnectionHelperExtensions
{
public static IServiceCollection AddDevHttpClient(this IServiceCollection services, int sslPort)
{
var devSslHelper = new DevHttpsConnectionHelper(sslPort);
var http = devSslHelper.HttpClient;
http.BaseAddress = new Uri(devSslHelper.DevServerRootUrl);
services.AddScoped(sp => http);
return services;
}
}
}