示例#1
0
        protected virtual async Task <SteamWebAccessRequest> ConstructLoginRequest(LoginCredentials credentials)
        {
            // Get a RSA public key for password encryption
            var serverResponse = await SteamWebAccess.FetchString(
                new SteamWebAccessRequest(
                    WebLoginRSAUrl,
                    SteamWebAccessRequestMethod.Post,
                    new QueryStringBuilder
            {
                { "donotcache", (DateTime.UtcNow - Epoch).TotalMilliseconds },
                { "username", credentials.UserName }
            }
                    )
                ).ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(serverResponse) ||
                serverResponse.Contains("<BODY>\nAn error occurred while processing your request."))
            {
                throw new UserLoginException(UserLoginErrorCode.GeneralFailure, this);
            }

            var rsaResponse = JsonConvert.DeserializeObject <RSAResponse>(serverResponse);

            if (rsaResponse?.Success != true)
            {
                throw new UserLoginException(UserLoginErrorCode.BadRSAResponse, this);
            }

            // Sleep for a bit to give Steam a chance to catch up??
            await Task.Delay(350).ConfigureAwait(false);

            string encryptedPassword;

            using (var rsaEncrypt = new RSACryptoServiceProvider())
            {
                rsaEncrypt.ImportParameters(new RSAParameters
                {
                    Exponent = HexStringToByteArray(rsaResponse.Exponent),
                    Modulus  = HexStringToByteArray(rsaResponse.Modulus)
                });

                encryptedPassword =
                    Convert.ToBase64String(
                        rsaEncrypt.Encrypt(
                            Encoding.UTF8.GetBytes(credentials.Password)
                            , false)
                        );
            }

            return(new SteamWebAccessRequest(
                       WebLoginUrl,
                       SteamWebAccessRequestMethod.Post,
                       new QueryStringBuilder
            {
                { "donotcache", (DateTime.UtcNow - Epoch).TotalMilliseconds },
                { "rsatimestamp", rsaResponse.Timestamp },
                { "password", encryptedPassword },
                { "username", credentials.UserName },
                { "twofactorcode", credentials.TwoFactorAuthenticationCode ?? "" },
                { "emailauth", RequiresEmailVerification ? (credentials.EmailVerificationCode ?? "") : "" },
                { "loginfriendlyname", "" },
                { "captchagid", RequiresCaptchaCode ? (CaptchaGID ?? "-1") : "-1" },
                { "captcha_text", RequiresCaptchaCode ? (credentials.CaptchaCode ?? "") : "" },
                {
                    "emailsteamid",
                    RequiresTwoFactorAuthenticationCode || RequiresEmailVerification
                            ? (SteamId?.ToString() ?? "")
                            : ""
                },
                { "rsatimestamp", rsaResponse.Timestamp },
                { "remember_login", "true" }
            }
                       ));
        }