Skip to content

Commit

Permalink
Use ServerCertificateCustomValidationCallback (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbritch authored Oct 4, 2024
1 parent 6a13bd2 commit 1afb580
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 71 deletions.
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

0 comments on commit 1afb580

Please sign in to comment.