public static OAuthAccessToken Get() { OAuthAccessToken accessToken = null; try { if (File.Exists(tknFile)) { var text = File.ReadAllText(tknFile); var content = SimplyDecrypt(text); var match = Regex.Match(content, "^(.+?),(.+?),(.+?),(.+?)$", RegexOptions.IgnoreCase); accessToken = new OAuthAccessToken(); AMicroblogAPI.Environment.AccessToken = accessToken; AMicroblogAPI.Environment.CurrentUserAccount = match.Groups[1].Value; accessToken.UserID = match.Groups[2].Value; accessToken.Token = match.Groups[3].Value; accessToken.Secret = match.Groups[4].Value; } } catch { // Nothing to do. } return accessToken; }
private void btnLogon_Click(object sender, RoutedEventArgs e) { try { gridMain.IsEnabled = false; lbMsg.Text = string.Empty; var name = txbUserName.Text.Trim(); var pwd = txbPwd.Password; if (string.IsNullOrEmpty(name)) { ShowMessage("Name not provided."); return; } if (string.IsNullOrEmpty(pwd)) { ShowMessage("Password not provided."); return; } var callback = new AsyncCallback<OAuthAccessToken>(delegate(AsyncCallResult<OAuthAccessToken> result) { if (result.Success) { Token = result.Data; this.Dispatcher.Invoke(new Action(delegate() { if (ckbSavePwd.IsChecked.HasValue && ckbSavePwd.IsChecked.Value) CredentialHelper.Save(name, Token); gridMain.IsEnabled = true; this.DialogResult = true; })); } else { this.Dispatcher.Invoke(new Action(delegate() { gridMain.IsEnabled = true; })); ShowMessage("Failed to login due to: {0}.", result.Exception.Message); } }); lbMsg.Text = "Login..."; AMicroblog.LoginAsync(callback, name, pwd); } catch (Exception ex) { ShowMessage(ex.Message); } }
public static void Save(string userName, OAuthAccessToken accessToken) { try { var content = string.Format("{0},{1},{2},{3}", userName, accessToken.UserID, accessToken.Token, accessToken.Secret); var encryptedText = SimplyEncrypt(content); File.WriteAllText(tknFile, encryptedText); } catch { // Nothing to do. } }