Ejemplo n.º 1
0
        private async Task RemoveIndices(T user)
        {
            TableUserIdIndex UserIdIndex = new TableUserIdIndex(user.UserName.Base64Encode(), user.Id);

            UserIdIndex.ETag = "*";
            TableUserEmailIndex EmailIndex = new TableUserEmailIndex(user.Email.Base64Encode(), user.Id);

            EmailIndex.ETag = "*";

            Task t1 = _userIndexTable.ExecuteAsync(TableOperation.Delete(UserIdIndex));
            Task t2 = _userEmailIndexTable.ExecuteAsync(TableOperation.Delete(EmailIndex));

            await Task.WhenAll(t1, t2);
        }
Ejemplo n.º 2
0
        public async Task <T> FindByEmailAsync(string email)
        {
            if (String.IsNullOrWhiteSpace(email))
            {
                return(null);
            }

            TableOperation retrieveIndexOp = TableOperation.Retrieve <TableUserEmailIndex>(email.Base64Encode(), "");
            TableResult    indexResult     = await _userEmailIndexTable.ExecuteAsync(retrieveIndexOp);

            if (indexResult.Result == null)
            {
                return(null);
            }
            TableUserEmailIndex userEmailIndex = (TableUserEmailIndex)indexResult.Result;

            return(await FindByIdAsync(userEmailIndex.UserId));
        }
Ejemplo n.º 3
0
        public async Task CreateAsync(T user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            user.SetPartitionAndRowKey();
            TableUserIdIndex indexItem      = new TableUserIdIndex(user.UserName.Base64Encode(), user.Id);
            TableOperation   indexOperation = TableOperation.Insert(indexItem);

            try
            {
                await _userIndexTable.ExecuteAsync(indexOperation);
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 409)
                {
                    throw new DuplicateUsernameException();
                }
                throw;
            }

            if (!String.IsNullOrWhiteSpace(user.Email))
            {
                TableUserEmailIndex emailIndexItem      = new TableUserEmailIndex(user.Email.Base64Encode(), user.Id);
                TableOperation      emailIndexOperation = TableOperation.Insert(emailIndexItem);
                try
                {
                    await _userEmailIndexTable.ExecuteAsync(emailIndexOperation);
                }
                catch (StorageException ex)
                {
                    try
                    {
                        indexItem.ETag = "*";
                        TableOperation deleteOperation = TableOperation.Delete(indexItem);
                        _userIndexTable.ExecuteAsync(deleteOperation).Wait();
                    }
                    catch (Exception)
                    {
                        // if we can't delete the index item throw out the exception below
                    }


                    if (ex.RequestInformation.HttpStatusCode == 409)
                    {
                        throw new DuplicateEmailException();
                    }
                    throw;
                }
            }

            try
            {
                if (user.LockoutEndDate < _minTableStoreDate)
                {
                    user.LockoutEndDate = _minTableStoreDate;
                }
                TableOperation operation = TableOperation.InsertOrReplace(user);
                await _userTable.ExecuteAsync(operation);

                if (user.Logins.Any())
                {
                    TableBatchOperation batch = new TableBatchOperation();
                    List <TableUserLoginProviderKeyIndex> loginIndexItems = new List <TableUserLoginProviderKeyIndex>();
                    foreach (TableUserLogin login in user.Logins)
                    {
                        login.UserId = user.Id;
                        login.SetPartitionAndRowKey();
                        batch.InsertOrReplace(login);

                        TableUserLoginProviderKeyIndex loginIndexItem = new TableUserLoginProviderKeyIndex(user.Id, login.ProviderKey, login.LoginProvider);
                        loginIndexItems.Add(loginIndexItem);
                    }
                    await _loginTable.ExecuteBatchAsync(batch);

                    // can't batch the index items as different primary keys
                    foreach (TableUserLoginProviderKeyIndex loginIndexItem in loginIndexItems)
                    {
                        await _loginProviderKeyIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(loginIndexItem));
                    }
                }
            }
            catch (Exception)
            {
                // attempt to delete the index item - needs work
                indexItem.ETag = "*";
                TableOperation deleteOperation = TableOperation.Delete(indexItem);
                _userIndexTable.ExecuteAsync(deleteOperation).Wait();
                throw;
            }
        }