private void btnLogin_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(txtUserName.Text) || string.IsNullOrEmpty(txtPassword.Password)) return; if (cmbLoginSite.SelectedIndex < 0) return; LoginHelper loginHelper = new LoginHelper(); LoginResult loginResult = loginHelper.Login(txtUserName.Text, txtPassword.Password, Enums.GetEnumName<LoginSite>(cmbLoginSite.SelectedValue.ToString())); txtStatus.Text = JsonConvert.SerializeObject(loginResult.Cookies); }
public override bool DoLogin(string userName, string password) { LoginHelper loginHelper = new LoginHelper(); LoginResult loginResult = loginHelper.Login(userName, password, LoginSite.Weibo); if (loginResult.Result != ResultType.Success) return false; //rebuild the cookie collection and get the token. CookieContainer cookies = HttpHelper.ConvertToCookieContainer(loginResult.Cookies); HttpResponse authorize = HttpHelper.HttpRequest(GetAuthorizationUrl(), cookies: cookies); Match matchCode = Regex.Match(authorize.Url, "code=(?<code>[^&]+)"); if (authorize.HttpCode == 200 && matchCode.Success && matchCode.Groups["code"].Success) { string code = matchCode.Groups["code"].Value; GetAccessTokenByCode(code); return true; } else { string postData = string.Format("action=submit&withOfficalFlag=0&ticket=&isLoginSina=&response_type=token®Callback=&redirect_uri={0}&client_id={1}&state=&from=&userId={2}&passwd={3}&display=js", Uri.EscapeDataString(string.IsNullOrEmpty(CallbackUrl) ? "" : CallbackUrl), Uri.EscapeDataString(ClientId), Uri.EscapeDataString(userName), Uri.EscapeDataString(password)); HttpResponse token = HttpHelper.HttpRequest(AUTH_URL, postData, cookies, null, GetAuthorizationUrl()); if (token.HttpCode == 200) { Match matchToken = Regex.Match(token.Content, "\"access_token\":\"(?<access_token>[^\"]+)\".*?uid\":\"(?<uid>[^\"]+)\""); if (matchToken.Success && matchToken.Groups["access_token"].Success) { AccessToken = matchToken.Groups["access_token"].Value; UID = matchToken.Groups["uid"].Value; isAccessTokenSet = true; return true; } } } return false; }