BaconographyWP8.PlatformServices.UserService.AddStoredCredential C# (CSharp) Method

AddStoredCredential() public method

public AddStoredCredential ( UserCredential newCredential, string password ) : System.Threading.Tasks.Task
newCredential BaconographyPortable.Model.Reddit.UserCredential
password string
return System.Threading.Tasks.Task
        public async Task AddStoredCredential(UserCredential newCredential, string password)
        {
            var userInfoDb = await GetUserInfoDB();

			try
			{
				var currentCredentials = await StoredCredentials();
				var existingCredential = currentCredentials.FirstOrDefault(credential => credential.Username == newCredential.Username);
				if (existingCredential != null)
				{
					var lastCookie = existingCredential.LoginCookie;
					//we already exist in the credentials, just update our login token and password (if its set)
					if (existingCredential.LoginCookie != newCredential.LoginCookie ||
						existingCredential.IsDefault != newCredential.IsDefault)
					{
						existingCredential.LoginCookie = newCredential.LoginCookie;
						existingCredential.IsDefault = newCredential.IsDefault;

						//go find the one we're updating and actually do it
						var userCredentialsCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "credentials", DBReadFlags.NoLock);
						if (userCredentialsCursor != null)
						{
							using (userCredentialsCursor)
							{
								do
								{
									var credential = JsonConvert.DeserializeObject<UserCredential>(userCredentialsCursor.GetString());
									if (credential.Username == newCredential.Username)
									{
										await userCredentialsCursor.UpdateAsync(JsonConvert.SerializeObject(existingCredential));
										break;
									}
								} while (await userCredentialsCursor.MoveNextAsync());
							}
						}
					}
					if (!string.IsNullOrWhiteSpace(password))
					{
						AddOrUpdateWindowsCredential(existingCredential, password, lastCookie);
					}
				}
				else
				{
					await userInfoDb.InsertAsync("credentials", JsonConvert.SerializeObject(newCredential));
					var newPassData = new PasswordData { Password = password, LastCookie = newCredential.LoginCookie };
					await userInfoDb.InsertAsync("passwords", JsonConvert.SerializeObject(newPassData));
					//force a re-get of the credentials next time someone wants them
					_storedCredentials = null;
				}
			}
			catch
			{
				//let it fail
			}
        }