示例#1
0
        /// <summary>
        /// Executes the web login using the Steam website.
        /// </summary>
        /// <param name="username">Username to use</param>
        /// <param name="password">Password to use</param>
        /// <param name="machineAuth">Steam machine auth string.
        /// <param name="userInputOutput">The user I/O handler to use. If null, defaults to Console.Write/Read</param>
        /// You should save Account.SteamMachineAuth after the code has been entered and the request has
        /// been successful and pass it to DoLogin in future attempts to login.
        /// This field can be left blank, but if the account is SteamGuard protected
        /// it will ask you for a new code every time.</param>
        /// <returns>An Account object to that contains the SteamId and AuthContainer.</returns>
        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);
        }
示例#2
0
        /// <summary>
        /// Executes the web login using the Steam website.
        /// </summary>
        /// <param name="username">Username to use</param>
        /// <param name="password">Password to use</param>
        /// <param name="machineAuth">Steam machine auth string. 
        /// You should save Web.SteamMachineAuth after the code has been entered and the request has 
        /// been successful and pass it to DoLogin in future attempts to login. 
        /// This field can be left blank, but if the account is SteamGuard protected 
        /// it will ask you for a new code every time.</param>
        /// <returns>An Account object to that contains the SteamId and AuthContainer.</returns>
        public Account DoLogin(string username, string password, string machineAuth = "")
        {
            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);
                var 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},
                    {"rememberlogin", "false"}
                };
                // Captcha
                string capText = string.Empty;
                if (captcha)
                {
                    Process.Start("https://steamcommunity.com/public/captcha.php?gid=" + loginJson.CaptchaGid);
                    Console.WriteLine(
                        "Please note, if you enter in your captcha correctly and it still opens up new captchas, double check your username and password.");
                    Console.Write("Please enter the numbers/letters from the picture that opened up: ");
                    capText = Console.ReadLine();
                }

                data.Add("captchagid", captcha ? capGid : string.Empty);
                data.Add("captcha_text", captcha ? capText : string.Empty);
                // Captcha end

                // SteamGuard
                if (steamGuard)
                {
                    Console.Write("SteamGuard code required: ");
                    steamGuardText = Console.ReadLine();
                    steamGuardId = loginJson.EmailSteamId;
                }

                data.Add("emailauth", steamGuardText);
                data.Add("emailsteamid", steamGuardId);
                // SteamGuard end

                //TwoFactor
                if (twoFactor && !loginJson.Success)
                {
                    Console.Write("TwoFactor code required: ");
                    twoFactorText = Console.ReadLine();
                }

                data.Add("twofactorcode", twoFactor ? twoFactorText : string.Empty);

                data.Add("rsatimestamp", time);

                CookieContainer cc = null;
                if (!string.IsNullOrEmpty(machineAuth))
                {
                    SteamMachineAuth = 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))
                {
                    //var steamStream = webResponse.GetResponseStream();

                    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);
                           SteamLogin = cookie.Value;
                            break;
                        case "steamLoginSecure":
                            account.AuthContainer.Add(cookie);
                            SteamLoginSecure = cookie.Value;
                            break;
                        case "timezoneOffset":
                            account.AuthContainer.Add(cookie);
                            TimezoneOffset = cookie.Value;
                            break;
                    }
                    if (!cookie.Name.StartsWith("steamMachineAuth")) continue;
                    SteamMachineAuth = cookie.Name + "=" + cookie.Value;
                }

                if (!string.IsNullOrEmpty(SteamMachineAuth))
                    account.AddMachineAuthCookies(SteamMachineAuth);

                SubmitCookies(_cookies);

                account.AuthContainer.Add(_cookies.GetCookies(new Uri("https://steamcommunity.com"))["sessionid"]);

                SessionId = _cookies.GetCookies(new Uri("https://steamcommunity.com"))["sessionid"]?.Value;

                return account;
            }
            Console.WriteLine("SteamWeb Error: " + loginJson.Message);
            return null;
        }