BaconographyW8.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();

            var currentCredentials = await StoredCredentials();
            var existingCredential = currentCredentials.FirstOrDefault(credential => credential.Username == newCredential.Username);
            if (existingCredential != null)
            {
                //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;

                    try
                    {
                        //go find the one we're updating and actually do it
                        var userCredentialsCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "credentials", DBReadFlags.AutoLock);
                        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());
                            }
                        }
                    }
                    catch
                    {
                        //let it fail
                    }
                }
                if (!string.IsNullOrWhiteSpace(password))
                {
                    AddOrUpdateWindowsCredential(existingCredential, password);
                }
            }
            else
            {
                await userInfoDb.InsertAsync("credentials", JsonConvert.SerializeObject(newCredential));
                //force a re-get of the credentials next time someone wants them
                _storedCredentials = null;
            }
        }