Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ServerCertificateCustomValidationCallback #525

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion 8.0/WebServices/TodoREST/TodoREST/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static MauiApp CreateMauiApp()
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

builder.Services.AddSingleton<IHttpsClientHandlerService, HttpsClientHandlerService>();
builder.Services.AddSingleton<IRestService, RestService>();
builder.Services.AddSingleton<ITodoService, TodoService>();

Expand Down

This file was deleted.

This file was deleted.

23 changes: 15 additions & 8 deletions 8.0/WebServices/TodoREST/TodoREST/Services/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ public class RestService : IRestService
{
HttpClient _client;
JsonSerializerOptions _serializerOptions;
IHttpsClientHandlerService _httpsClientHandlerService;

public List<TodoItem> Items { get; private set; }

public RestService(IHttpsClientHandlerService service)
public RestService()
{
#if DEBUG
_httpsClientHandlerService = service;
HttpMessageHandler handler = _httpsClientHandlerService.GetPlatformMessageHandler();
if (handler != null)
_client = new HttpClient(handler);
else
_client = new HttpClient();
HttpClientHandler insecureHandler = GetInsecureHandler();
_client = new HttpClient(insecureHandler);
#else
_client = new HttpClient();
#endif
Expand All @@ -32,6 +27,18 @@ public RestService(IHttpsClientHandlerService service)
};
}

private HttpClientHandler GetInsecureHandler()
{
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
};
return handler;
}

public async Task<List<TodoItem>> RefreshDataAsync()
{
Items = new List<TodoItem>();
Expand Down
Loading