public Account DoLogin(string username, string password, string machineAuth = "", IUserInputOutputHandler userInputOutput = null)
{
if (userInputOutput == null) userInputOutput = new ConsoleInputOutput();
Thread.Sleep(2000);
var rsaHelper = new RsaHelper(password);
var loginDetails = new Dictionary<string, string> { { "username", username } };
IResponse response = Fetch("https://steamcommunity.com/login/getrsakey", "POST", loginDetails);
string encryptedBase64Password = rsaHelper.EncryptPassword(response);
if (encryptedBase64Password == null) return null;
LoginResult loginJson = null;
CookieCollection cookieCollection;
string steamGuardText = string.Empty;
string steamGuardId = string.Empty;
string twoFactorText = string.Empty;
do
{
bool captcha = loginJson != null && loginJson.CaptchaNeeded;
bool steamGuard = loginJson != null && loginJson.EmailAuthNeeded;
bool twoFactor = loginJson != null && loginJson.RequiresTwofactor;
var time = Uri.EscapeDataString(rsaHelper.RsaJson.TimeStamp);
string capGid = "-1";
if (loginJson != null && loginJson.CaptchaNeeded)
capGid = Uri.EscapeDataString(loginJson.CaptchaGid);
var data = new Dictionary<string, string>
{
{"password", encryptedBase64Password},
{"username", username},
{"loginfriendlyname", string.Empty},
{"remember_login", "false"}
};
// Captcha
string capText = string.Empty;
if (captcha)
{
Process.Start("https://steamcommunity.com/public/captcha.php?gid=" + loginJson.CaptchaGid);
capText =
userInputOutput.GetInput(
"Please note, if you enter in your captcha correctly and it still opens up new captchas, double check your username and password.\n Please enter the numbers/letters from the picture that opened up: ",
"Captcha");
}
data.Add("captchagid", capGid);
data.Add("captcha_text", captcha ? capText : string.Empty);
// Captcha end
// SteamGuard
if (steamGuard)
{
steamGuardText = userInputOutput.GetInput("SteamGuard code required: ", "SteamGuard");
steamGuardId = loginJson.EmailSteamId;
}
data.Add("emailauth", steamGuardText);
data.Add("emailsteamid", steamGuardId);
// SteamGuard end
//TwoFactor
if (twoFactor && !loginJson.Success)
{
twoFactorText = userInputOutput.GetInput("TwoFactor code required: ", "Two Factor Authentication");
}
data.Add("twofactorcode", twoFactor ? twoFactorText : string.Empty);
data.Add("rsatimestamp", time);
CookieContainer cc = null;
if (!string.IsNullOrEmpty(machineAuth))
{
cc = new CookieContainer();
var split = machineAuth.Split('=');
var machineCookie = new Cookie(split[0], split[1]);
cc.Add(new Uri("https://steamcommunity.com/login/dologin/"), machineCookie);
}
using (IResponse webResponse = Fetch("https://steamcommunity.com/login/dologin/", "POST", data, cc))
{
string json = webResponse.ReadStream();
loginJson = JsonConvert.DeserializeObject<LoginResult>(json);
cookieCollection = webResponse.Cookies;
}
} while (loginJson.CaptchaNeeded || loginJson.EmailAuthNeeded || (loginJson.RequiresTwofactor && !loginJson.Success));
Account account;
if (loginJson.EmailSteamId != null)
account = new Account(Convert.ToUInt64(loginJson.EmailSteamId));
else if (loginJson.TransferParameters?.Steamid != null)
account = new Account(Convert.ToUInt64(loginJson.TransferParameters.Steamid));
else
return null;
if (loginJson.Success)
{
_cookies = new CookieContainer();
foreach (Cookie cookie in cookieCollection)
{
_cookies.Add(cookie);
switch (cookie.Name)
{
case "steamLogin":
account.AuthContainer.Add(cookie);
break;
case "steamLoginSecure":
account.AuthContainer.Add(cookie);
break;
case "timezoneOffset":
account.AuthContainer.Add(cookie);
break;
}
if (cookie.Name.StartsWith("steamMachineAuth"))
{
account.SteamMachineAuth = cookie.Name + "=" + cookie.Value;
}
else if (!string.IsNullOrEmpty(machineAuth))
{
account.SteamMachineAuth = machineAuth;
}
}
SubmitCookies(_cookies);
// ReSharper disable once AssignNullToNotNullAttribute
account.AuthContainer.Add(_cookies.GetCookies(new Uri("https://steamcommunity.com"))["sessionid"]);
return account;
}
userInputOutput.OutputMessage("SteamWeb Error: " + loginJson.Message);
return null;
}