Skip to content

Commit

Permalink
Clear autologon credentials on session log-on
Browse files Browse the repository at this point in the history
  • Loading branch information
starcraft66 committed Jun 18, 2024
1 parent 9a3c6c2 commit 0d022fc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions Lanpartyseating.Desktop.Abstractions/BaseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions Lanpartyseating.Desktop.Abstractions/ClearAutoLogonRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Lanpartyseating.Desktop.Abstractions;

public class ClearAutoLogonRequest : BaseMessage
{
}
9 changes: 6 additions & 3 deletions Lanpartyseating.Desktop.Tray/ToastNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions Lanpartyseating.Desktop/Business/DummySessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
1 change: 1 addition & 0 deletions Lanpartyseating.Desktop/Business/ISessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public interface ISessionManager
public void SignInGamerAccount();
public void SignInTournamentAccount();
public void SignOut();
public void ClearAutoLogonCredentials();
}
12 changes: 11 additions & 1 deletion Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public class NamedPipeServerHostedService : BackgroundService, INamedPipeServerS
{
private readonly ILogger<NamedPipeServerHostedService> _logger;
private readonly ReservationManager _reservationManager;
private readonly ISessionManager _sessionManager;
private const string PipeName = "Lanpartyseating.Desktop";
private NamedPipeServerStream? _server;

public NamedPipeServerHostedService(ILogger<NamedPipeServerHostedService> logger, ReservationManager reservationManager)
public NamedPipeServerHostedService(ILogger<NamedPipeServerHostedService> logger, ReservationManager reservationManager, ISessionManager sessionManager)
{
_logger = logger;
_reservationManager = reservationManager;
_sessionManager = sessionManager;
_server = null;
}

Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions Lanpartyseating.Desktop/Business/WindowsSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0d022fc

Please sign in to comment.