示例#1
0
        private bool LoginWithUsername()
        {
            var userName = Config.GetParameterValue("Username") as string;
            var password = Config.GetParameterValue("Password") as string;

            if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(password))
            {
                IsAnonymous = true;
                ResetAuthData();
                return(true);
            }

            NickName = userName;

            var authString = String.Format(@"nickname={0}&password={1}&remember=1", HttpUtility.UrlEncode(userName), HttpUtility.UrlEncode(password));

            webClient.ContentType = ContentType.UrlEncoded;
            webClient.Headers["X-Requested-With"] = "XMLHttpRequest";

            webClient.Upload(@"http://goodgame.ru/ajax/login/", authString);
            var authToken = webClient.CookieValue("PHPSESSID", "http://goodgame.ru");
            var uid       = webClient.CookieValue("uid", "http://goodgame.ru");

            if (String.IsNullOrWhiteSpace(authToken))
            {
                Log.WriteError("Login to goodgame.ru failed. Joining anonymously");
                IsAnonymous = true;
                ResetAuthData();
                return(false);
            }
            else
            {
                Config.SetParameterValue("AuthToken", authToken);
                Config.SetParameterValue("AuthTokenCredentials", userName + password);
                Config.SetParameterValue("UserId", uid);
                if (!String.IsNullOrEmpty(uid) && uid != "0")
                {
                    return(LoginWithToken());
                }
                else
                {
                    return(false);
                }
            }
        }
示例#2
0
        public bool LoginWithUsername()
        {
            var userName = Config.GetParameterValue("Username") as string;
            var password = Config.GetParameterValue("Password") as string;

            if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(password) || userName.Equals(AnonymousNickName, StringComparison.InvariantCultureIgnoreCase))
            {
                IsAnonymous = true;
                return(true);
            }

            if (Regex.IsMatch(userName, @"justinfan\d+"))
            {
                AnonymousNickName = userName;
            }

            NickName = userName;

            webClient.SetCookie("api_token", null, "twitch.tv");
            webClient.SetCookie("csrf_token", null, "twitch.tv");

            var csrfToken = this.With(x => webClient.Download("http://www.twitch.tv/login"))
                            .With(x => Re.GetSubString(x, @"^.*authenticity_token.*?value=""(.*?)"""));


            if (csrfToken == null)
            {
                Log.WriteError("Twitch: Can't get CSRF token. Twitch web layout changed ?");
                return(false);
            }
            string csrf_cookie = csrfToken;

            if (csrf_cookie.Substring(csrf_cookie.Length - 1).Equals("="))
            {
                csrf_cookie = csrf_cookie.Substring(0, csrf_cookie.Length - 1) + "%3D";
            }

            webClient.SetCookie("csrf_token", csrf_cookie, "twitch.tv");
            webClient.ContentType = ContentType.UrlEncoded;
            webClient.Headers["X-Requested-With"] = "XMLHttpRequest";
            webClient.Headers["X-CSRF-Token"]     = csrfToken;
            webClient.Headers["Accept"]           = "text/html, application/xhtml+xml, */*";

            var apiToken = this.With(x => webClient.Upload("https://secure.twitch.tv/user/login", String.Format(
                                                               "utf8=%E2%9C%93&authenticity_token={0}%3D&redirect_on_login=&embed_form=false&user%5Blogin%5D={1}&user%5Bpassword%5D={2}",
                                                               csrfToken,
                                                               userName,
                                                               password)))
                           .With(x => webClient.CookieValue("api_token", "http://twitch.tv"));

            if (String.IsNullOrWhiteSpace(apiToken))
            {
                Log.WriteError("Twitch: Can't get API token");
                return(false);
            }
            webClient.Headers["Twitch-Api-Token"] = apiToken;
            webClient.Headers["X-CSRF-Token"]     = csrfToken;
            webClient.Headers["Accept"]           = "*/*";

            if (apiToken == null)
            {
                Log.WriteError("Login to twitch.tv failed. Joining anonymously");
                IsAnonymous = true;
                return(false);
            }
            else
            {
                var oauthToken = this.With(x => webClient.Download("http://api.twitch.tv/api/me?on_site=1"))
                                 .With(x => JToken.Parse(x))
                                 .With(x => x.Value <string>("chat_oauth_token"));

                if (String.IsNullOrWhiteSpace(oauthToken))
                {
                    Log.WriteError("Login to twitch.tv failed. Joining anonymously");
                    IsAnonymous = true;
                    return(false);
                }

                IsAnonymous = false;
                Config.SetParameterValue("OAuthToken", oauthToken);
                Config.SetParameterValue("ApiToken", apiToken);
                Config.SetParameterValue("AuthTokenCredentials", userName + password);

                return(LoginWithToken());
            }
        }