Program.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using SteamAuth;
  3. using Newtonsoft.Json;
  4. using System.IO;
  5. using SteamKit2;
  6. using SteamKit2.Authentication;
  7. using SteamKit2.Internal;
  8. using System.Threading.Tasks;
  9. namespace TestBed
  10. {
  11. class Program
  12. {
  13. static async Task Main(string[] args)
  14. {
  15. //This basic loop will log into user accounts you specify, enable the mobile authenticator, and save a maFile (mobile authenticator file)
  16. while (true)
  17. {
  18. // Start a new SteamClient instance
  19. SteamClient steamClient = new SteamClient();
  20. // Connect to Steam
  21. steamClient.Connect();
  22. // Really basic way to wait until Steam is connected
  23. while (!steamClient.IsConnected)
  24. await Task.Delay(500);
  25. Console.WriteLine("Enter username: ");
  26. string username = Console.ReadLine();
  27. Console.WriteLine("Enter password: ");
  28. string password = Console.ReadLine();
  29. // Create a new auth session
  30. CredentialsAuthSession authSession;
  31. try
  32. {
  33. authSession = await steamClient.Authentication.BeginAuthSessionViaCredentialsAsync(new AuthSessionDetails
  34. {
  35. Username = username,
  36. Password = password,
  37. IsPersistentSession = false,
  38. PlatformType = EAuthTokenPlatformType.k_EAuthTokenPlatformType_MobileApp,
  39. ClientOSType = EOSType.Android9,
  40. Authenticator = new UserConsoleAuthenticator(),
  41. });
  42. }
  43. catch (Exception ex)
  44. {
  45. Console.WriteLine("Error logging in: " + ex.Message);
  46. return;
  47. }
  48. // Starting polling Steam for authentication response
  49. var pollResponse = await authSession.PollingWaitForResultAsync();
  50. // Build a SessionData object
  51. SessionData sessionData = new SessionData()
  52. {
  53. SteamID = authSession.SteamID.ConvertToUInt64(),
  54. AccessToken = pollResponse.AccessToken,
  55. RefreshToken = pollResponse.RefreshToken,
  56. };
  57. // Init AuthenticatorLinker
  58. AuthenticatorLinker linker = new AuthenticatorLinker(sessionData);
  59. Console.WriteLine("If account has no phone number, enter one now: (+1 XXXXXXXXXX)");
  60. string phoneNumber = Console.ReadLine();
  61. if (string.IsNullOrWhiteSpace(phoneNumber))
  62. {
  63. linker.PhoneNumber = null;
  64. }
  65. else
  66. {
  67. linker.PhoneNumber = phoneNumber;
  68. }
  69. int tries = 0;
  70. AuthenticatorLinker.LinkResult result = AuthenticatorLinker.LinkResult.GeneralFailure;
  71. while (tries <= 5)
  72. {
  73. tries++;
  74. // Add authenticator
  75. result = await linker.AddAuthenticator();
  76. if (result == AuthenticatorLinker.LinkResult.MustConfirmEmail)
  77. {
  78. Console.WriteLine("Click the link sent to your email address: " + linker.ConfirmationEmailAddress);
  79. Console.WriteLine("Press enter when done");
  80. Console.ReadLine();
  81. continue;
  82. }
  83. if (result == AuthenticatorLinker.LinkResult.MustProvidePhoneNumber)
  84. {
  85. Console.WriteLine("Account requires a phone number. Login again and enter one.");
  86. break;
  87. }
  88. if (result == AuthenticatorLinker.LinkResult.AuthenticatorPresent)
  89. {
  90. Console.WriteLine("Account already has an authenticator linked.");
  91. break;
  92. }
  93. if (result != AuthenticatorLinker.LinkResult.AwaitingFinalization)
  94. {
  95. Console.WriteLine("Failed to add authenticator: " + result);
  96. break;
  97. }
  98. // Write maFile
  99. try
  100. {
  101. string sgFile = JsonConvert.SerializeObject(linker.LinkedAccount, Formatting.Indented);
  102. string fileName = linker.LinkedAccount.AccountName + ".maFile";
  103. File.WriteAllText(fileName, sgFile);
  104. break;
  105. }
  106. catch (Exception e)
  107. {
  108. Console.WriteLine(e.Message);
  109. Console.WriteLine("EXCEPTION saving maFile. For security, authenticator will not be finalized.");
  110. break;
  111. }
  112. }
  113. if (result != AuthenticatorLinker.LinkResult.AwaitingFinalization)
  114. continue;
  115. tries = 0;
  116. while (tries <= 5)
  117. {
  118. Console.WriteLine("Please enter SMS code: ");
  119. string smsCode = Console.ReadLine();
  120. var linkResult = await linker.FinalizeAddAuthenticator(smsCode);
  121. if (linkResult != AuthenticatorLinker.FinalizeResult.Success)
  122. {
  123. Console.WriteLine("Failed to finalize authenticator: " + linkResult);
  124. continue;
  125. }
  126. Console.WriteLine("Authenticator finalized!");
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. }