Posted at 12-08-2026, 01:53 PM
using System;
using System.Threading.Tasks;
using ValNet;
using ValNet.Objects.Authentication;
public class RiotAuthenticator
{
private readonly string _username;
private readonly string _password;
// Delegate for retrieving the 2FA code (e.g., from user input or SMS)
public Func<Task<string>> TwoFactorCodeProvider { get; set; }
public RiotAuthenticator(string username, string password)
{
_username = username ?? throw new ArgumentNullException(nameof(username));
_password = password ?? throw new ArgumentNullException(nameof(password));
}
public async Task<bool> AuthenticateAsync()
{
var loginData = new RiotLoginData
{
username = _username,
password = _password
};
var user = new RiotUser(loginData);
try
{
Console.WriteLine($"Attempting to authenticate {_username}...");
var response = await user.Authentication.AuthenticateWithCloud();
if (response.bIsAuthComplete)
{
Console.WriteLine($"✅ Authentication successful for {_username}.");
return true;
}
if (response.type == "multifactor")
{
Console.WriteLine($"📧 2FA required. Code sent to: {response.multifactorData.email}");
return await HandleTwoFactorAuthentication(user, response);
}
// Handle other response types if needed (e.g., "rate_limit", "captcha")
Console.WriteLine($"⚠️ Authentication failed with response type: {response.type}");
return false;
}
catch (ValNetException ex)
{
// ValNet-specific errors (e.g., invalid credentials, network timeouts)
Console.WriteLine($"❌ ValNet error: {ex.Message}");
return false;
}
catch (Exception ex)
{
// Catch-all for unexpected issues
Console.WriteLine($"❌ Unexpected error: {ex.Message}");
return false;
}
}
private async Task<bool> HandleTwoFactorAuthentication(RiotUser user, AuthenticationResponse response)
{
if (TwoFactorCodeProvider == null)
{
Console.WriteLine("❌ 2FA required but no code provider was set.");
return false;
}
string code;
int retries = 3;
do
{
Console.Write("Enter 2FA code: ");
code = await TwoFactorCodeProvider.Invoke(); // Could also be Console.ReadLine() directly
try
{
var twoFactorResponse = await user.Authentication.AuthenticateTwoFactorCode(code);
if (twoFactorResponse.bIsAuthComplete)
{
Console.WriteLine($"✅ 2FA successful for {_username}.");
return true;
}
Console.WriteLine("❌ Invalid 2FA code, please try again.");
}
catch (ValNetException ex)
{
Console.WriteLine($"❌ 2FA error: {ex.Message}");
// If error indicates wrong code, we can retry; otherwise break.
if (ex.Message.Contains("invalid") || ex.Message.Contains("expired"))
{
// continue retry loop
}
else
{
return false;
}
}
retries--;
} while (retries > 0);
Console.WriteLine("❌ Too many failed 2FA attempts.");
return false;
}
}