private async void LoadStoredCredentials()
		{
			try
			{
				var notLoggedIn = new UserCredential { Username = UnselectedUsername, IsDefault = false };
				
				var storedCredentials = await SnooStreamViewModel.UserCredentialService.StoredCredentials();
				foreach (var credential in storedCredentials)
				{
					StoredCredentials.Add(credential);
				}

				StoredCredentials.Add(notLoggedIn);
				_selectedCredential = StoredCredentials.FirstOrDefault(stored => string.Compare(stored.Username, SnooStreamViewModel.RedditUserState.Username, StringComparison.CurrentCultureIgnoreCase) == 0) ?? notLoggedIn;

				IsAutoLogin = _selectedCredential == notLoggedIn || notLoggedIn.IsDefault;

				RaisePropertyChanged("StoredCredentials");
				RaisePropertyChanged("HasStoredLogins");
				RaisePropertyChanged("SelectedCredential");
			}
			catch (Exception ex)
			{
				//_logger.Error("failed loading stored credentials", ex);
			}
		}
        private void AddStoredCredentialImpl(UserCredential newCredential)
        {
            var userInfoDb = GetUserInfoDB();

            try
            {
                var currentCredentials = GetStoredCredentialsImpl();
                var existingCredential = currentCredentials.FirstOrDefault(credential => credential.Username == newCredential.Username);
                if (existingCredential != null)
                {
                    UpdateWindowsCredential(existingCredential);
				}
                else
                {
                    userInfoDb.Insert("credentials", JsonConvert.SerializeObject(newCredential));
                }
            }
            catch
            {
                //let it fail
            }
        }
		public async void FinishOAuth(string code)
		{
			try
			{
				_continue = new TaskCompletionSource<bool>();
				Finished = false;
				SnooStreamViewModel.NavigationService.NavigateToOAuthLanding(this);
				Working = true;
				var oAuth = await SnooStreamViewModel.RedditService.RequestGrantCode(code, _token);
				SnooStreamViewModel.RedditUserState.OAuth = oAuth;
				var currentAccount = await SnooStreamViewModel.RedditService.GetIdentity();
				ResultText = "Successfully logged in as " + currentAccount.Name;
				IsMod = currentAccount.IsMod;
				SnooStreamViewModel.RedditUserState.IsGold = currentAccount.IsGold;
				SnooStreamViewModel.RedditUserState.LoginCookie = "";
				SnooStreamViewModel.RedditUserState.ModHash = "";
				SnooStreamViewModel.RedditUserState.NeedsCaptcha = false;
				SnooStreamViewModel.RedditUserState.Username = currentAccount.Name;
				Success = true;
				Finished = true;
				//wait for the user to press continue so we can gather the default/store info
				await _continue.Task;
				SnooStreamViewModel.RedditUserState.IsDefault = IsAutoLogin;
				if (IsRememberLogin)
				{
                    var newCredential = new UserCredential { Username = Username, IsDefault = IsAutoLogin, Me = new Thing { Data = currentAccount, Kind = "t2" }, OAuth = oAuth };
                    await SnooStreamViewModel.UserCredentialService.AddStoredCredential(newCredential);
                    StoredCredentials.Add(newCredential);

                    //dont fire the login stuff just use the field directly
                    _selectedCredential = newCredential;

                    RaisePropertyChanged("HasStoredLogins");
                    RaisePropertyChanged("SelectedCredential");
				}
				GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<UserLoggedInMessage>(new UserLoggedInMessage { IsDefault = IsAutoLogin, NewAccount = currentAccount });
			}
			catch (Exception ex)
			{
				Success = false;
				ResultText = ex.Message;
			}
			finally
			{
				Working = false;
			}
			if (Success)
			{
				Finished = true;
			}
		}
        private void UpdateWindowsCredential(UserCredential existingCredential)
        {
            var userInfoDb = GetUserInfoDB();
            try
            {

				var userCredentialsCursor = userInfoDb.Select(userInfoDb.GetKeys().First(), "credentials", DBReadFlags.AutoLock);
				if (userCredentialsCursor != null)
				{
					using (userCredentialsCursor)
					{
						do
						{
							var credential = JsonConvert.DeserializeObject<UserCredential>(userCredentialsCursor.GetString());
							if (credential.Username == existingCredential.Username)
							{
								userCredentialsCursor.Update(JsonConvert.SerializeObject(existingCredential));
							}
						} while (userCredentialsCursor.MoveNext());
					}
				}
            }
            catch
            {
                //let it fail
            }
        }
 public Task AddStoredCredential(UserCredential newCredential)
 {
     return Task.Run(() => AddStoredCredentialImpl(newCredential));
 }