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

Clear autologon credentials on session log-on #20

Merged
merged 1 commit into from
Jun 18, 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: 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 @@
{
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 @@ -74,7 +76,7 @@

try
{
var waitTask = _server.WaitForConnectionAsync(stoppingToken);

Check warning on line 79 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 79 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 79 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Dereference of a possibly null reference.

Check warning on line 79 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Dereference of a possibly null reference.
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); // Adjust the timeout as needed

if (await Task.WhenAny(waitTask, timeoutTask) == timeoutTask)
Expand All @@ -99,7 +101,7 @@
}
finally
{
if (_server.IsConnected)

Check warning on line 104 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 104 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 104 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Dereference of a possibly null reference.

Check warning on line 104 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Dereference of a possibly null reference.
{
_server.Disconnect(); // Ensure the server is disconnected after handling a connection
}
Expand Down Expand Up @@ -167,13 +169,13 @@

while (!stoppingToken.IsCancellationRequested && _server.IsConnected)
{
string json = null;

Check warning on line 172 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 172 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 172 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 172 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Converting null literal or possible null value to non-nullable type.

try
{
if (_server.CanRead)
{
json = await reader.ReadLineAsync();

Check warning on line 178 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Lanpartyseating.Desktop/Business/NamedPipeServerHostedService.cs

View workflow job for this annotation

GitHub Actions / build (arm64)

Converting null literal or possible null value to non-nullable type.
}
}
catch (IOException ex)
Expand All @@ -196,6 +198,14 @@
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);
}
}
Loading