From 0d022fcfc74a1ebbbc15b79c26f4c6a3f363c7d0 Mon Sep 17 00:00:00 2001 From: Tristan Gosselin-Hane Date: Tue, 18 Jun 2024 00:21:48 -0400 Subject: [PATCH] Clear autologon credentials on session log-on --- .../BaseMessage.cs | 1 + .../ClearAutoLogonRequest.cs | 5 +++++ .../ToastNotificationService.cs | 9 ++++++--- .../Business/DummySessionManager.cs | 5 +++++ .../Business/ISessionManager.cs | 1 + .../Business/NamedPipeServerHostedService.cs | 12 +++++++++++- .../Business/WindowsSessionManager.cs | 14 ++++++++++++++ 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 Lanpartyseating.Desktop.Abstractions/ClearAutoLogonRequest.cs diff --git a/Lanpartyseating.Desktop.Abstractions/BaseMessage.cs b/Lanpartyseating.Desktop.Abstractions/BaseMessage.cs index 5b15289..7dce470 100644 --- a/Lanpartyseating.Desktop.Abstractions/BaseMessage.cs +++ b/Lanpartyseating.Desktop.Abstractions/BaseMessage.cs @@ -4,6 +4,7 @@ namespace Lanpartyseating.Desktop.Abstractions; [JsonDerivedType(typeof(ReservationStateRequest), typeDiscriminator: "sessionstaterequest")] [JsonDerivedType(typeof(ReservationStateResponse), typeDiscriminator: "sessionstateresponse")] +[JsonDerivedType(typeof(ClearAutoLogonRequest), typeDiscriminator: "clearautologonrequest")] [JsonDerivedType(typeof(TextMessage), typeDiscriminator: "textmessage")] public abstract class BaseMessage { diff --git a/Lanpartyseating.Desktop.Abstractions/ClearAutoLogonRequest.cs b/Lanpartyseating.Desktop.Abstractions/ClearAutoLogonRequest.cs new file mode 100644 index 0000000..60bdbc5 --- /dev/null +++ b/Lanpartyseating.Desktop.Abstractions/ClearAutoLogonRequest.cs @@ -0,0 +1,5 @@ +namespace Lanpartyseating.Desktop.Abstractions; + +public class ClearAutoLogonRequest : BaseMessage +{ +} \ No newline at end of file diff --git a/Lanpartyseating.Desktop.Tray/ToastNotificationService.cs b/Lanpartyseating.Desktop.Tray/ToastNotificationService.cs index f99aae0..47b82fe 100644 --- a/Lanpartyseating.Desktop.Tray/ToastNotificationService.cs +++ b/Lanpartyseating.Desktop.Tray/ToastNotificationService.cs @@ -70,9 +70,12 @@ private void ShowInitialTaskDialog(string heading, string text) private async Task SendInitialMessageAsync(NamedPipeClientStream client, CancellationToken stoppingToken) { await using var writer = new StreamWriter(client, leaveOpen: true); - var request = new ReservationStateRequest(); - var jsonRequest = JsonMessageSerializer.Serialize(request); - await writer.WriteLineAsync(jsonRequest); + var reservationStateRequest = new ReservationStateRequest(); + var jsonReservationStateRequest = JsonMessageSerializer.Serialize(reservationStateRequest); + var clearAutoLogonRequest = new ClearAutoLogonRequest(); + var jsonClearAutoLogonRequest = JsonMessageSerializer.Serialize(clearAutoLogonRequest); + await writer.WriteLineAsync(jsonReservationStateRequest); + await writer.WriteLineAsync(jsonClearAutoLogonRequest); await writer.FlushAsync(stoppingToken); } diff --git a/Lanpartyseating.Desktop/Business/DummySessionManager.cs b/Lanpartyseating.Desktop/Business/DummySessionManager.cs index 8102da6..a92d260 100644 --- a/Lanpartyseating.Desktop/Business/DummySessionManager.cs +++ b/Lanpartyseating.Desktop/Business/DummySessionManager.cs @@ -26,4 +26,9 @@ public void SignOut() { _logger.LogInformation("The client would have logged out an the current interactive session now"); } + + public void ClearAutoLogonCredentials() + { + _logger.LogInformation("The client would have cleared the autologon credentials now"); + } } \ No newline at end of file diff --git a/Lanpartyseating.Desktop/Business/ISessionManager.cs b/Lanpartyseating.Desktop/Business/ISessionManager.cs index f3ba091..d548e95 100644 --- a/Lanpartyseating.Desktop/Business/ISessionManager.cs +++ b/Lanpartyseating.Desktop/Business/ISessionManager.cs @@ -5,4 +5,5 @@ public interface ISessionManager public void SignInGamerAccount(); public void SignInTournamentAccount(); public void SignOut(); + public void ClearAutoLogonCredentials(); } \ No newline at end of file diff --git a/Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs b/Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs index 7f66c47..b51cc3f 100644 --- a/Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs +++ b/Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs @@ -15,13 +15,15 @@ public class NamedPipeServerHostedService : BackgroundService, INamedPipeServerS { private readonly ILogger _logger; private readonly ReservationManager _reservationManager; + private readonly ISessionManager _sessionManager; private const string PipeName = "Lanpartyseating.Desktop"; private NamedPipeServerStream? _server; - public NamedPipeServerHostedService(ILogger logger, ReservationManager reservationManager) + public NamedPipeServerHostedService(ILogger logger, ReservationManager reservationManager, ISessionManager sessionManager) { _logger = logger; _reservationManager = reservationManager; + _sessionManager = sessionManager; _server = null; } @@ -196,6 +198,14 @@ private async Task ProcessClientConnectionAsync(CancellationToken stoppingToken) await writer.WriteLineAsync(JsonMessageSerializer.Serialize(response)); await writer.FlushAsync(); } + else if (baseMessage is ClearAutoLogonRequest) + { + _sessionManager.ClearAutoLogonCredentials(); + } + else + { + _logger.LogWarning("Received an unknown message type."); + } // Check for cancellation again after processing the message stoppingToken.ThrowIfCancellationRequested(); diff --git a/Lanpartyseating.Desktop/Business/WindowsSessionManager.cs b/Lanpartyseating.Desktop/Business/WindowsSessionManager.cs index 1a4f7fb..3fef7e1 100644 --- a/Lanpartyseating.Desktop/Business/WindowsSessionManager.cs +++ b/Lanpartyseating.Desktop/Business/WindowsSessionManager.cs @@ -69,4 +69,18 @@ internal void LogoffInteractiveSession() var sessionId = WTSGetActiveConsoleSessionId(); WTSLogoffSession(IntPtr.Zero, sessionId, false); } + + public void ClearAutoLogonCredentials() + { + var winlogonRegPath = @"Software\Microsoft\Windows NT\CurrentVersion\Winlogon"; + + // Disable autologon + Registry.SetValue($@"HKEY_LOCAL_MACHINE\{winlogonRegPath}", "AutoAdminLogon", 0, RegistryValueKind.DWord); + + // Clear autologon username + Registry.SetValue($@"HKEY_LOCAL_MACHINE\{winlogonRegPath}", "DefaultUserName", "", RegistryValueKind.String); + + // Clear autologon password + Registry.SetValue($@"HKEY_LOCAL_MACHINE\{winlogonRegPath}", "DefaultPassword", "", RegistryValueKind.String); + } } \ No newline at end of file