Beispiel #1
0
        async Task RemoveIndices(T user)
        {
            var userIdIndex = new UserNameIndexEntity(user.UserName.ToSha256(), user.Id)
            {
                ETag = "*"
            };
            var userEmailIndex = new UserEmailIndexEntity(user.Email.ToSha256(), user.Id)
            {
                ETag = "*"
            };

            Task t1 = _userNameIndexTable.ExecuteAsync(TableOperation.Delete(userIdIndex));
            Task t2 = _userEmailIndexTable.ExecuteAsync(TableOperation.Delete(userEmailIndex));

            await Task.WhenAll(t1, t2);
        }
Beispiel #2
0
        public async Task <T> FindByNameAsync(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException("userName");
            }

            TableQuery <UserNameIndexEntity> indexQuery = new TableQuery <UserNameIndexEntity>()
                                                          .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, userName.ToSha256())).Take(1);

            IList <UserNameIndexEntity> matches = await ExecuteQueryAsync(_userTable, indexQuery, x => x);

            UserNameIndexEntity userNameIndex = matches.SingleOrDefault();

            if (userNameIndex == null)
            {
                return(null);
            }

            TableQuery <T> query = new TableQuery <T>()
                                   .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, userNameIndex.UserId)).Take(1);

            return((await ExecuteQueryAsync(_userTable, query, x => x)).SingleOrDefault());
        }
Beispiel #3
0
        public async Task CreateAsync(T user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            user.SetPartitionAndRowKey();

            var            userNameIndex  = new UserNameIndexEntity(user.UserName.ToSha256(), user.Id);
            TableOperation indexOperation = TableOperation.Insert(userNameIndex);

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

                throw;
            }

            if (!String.IsNullOrWhiteSpace(user.Email))
            {
                var            userEmailIndexEntity = new UserEmailIndexEntity(user.Email.ToSha256(), user.Id);
                TableOperation emailIndexOperation  = TableOperation.Insert(userEmailIndexEntity);
                try
                {
                    await _userEmailIndexTable.ExecuteAsync(emailIndexOperation);
                }
                catch (StorageException ex)
                {
                    try
                    {
                        userNameIndex.ETag = "*";
                        TableOperation deleteOperation = TableOperation.Delete(userNameIndex);
                        _userNameIndexTable.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())
                {
                    var batch           = new TableBatchOperation();
                    var loginIndexItems = new List <UserLoginProviderKeyIndexEntity>();
                    foreach (UserLoginEntity login in user.Logins)
                    {
                        login.UserId = user.Id;
                        login.SetPartitionKeyRowKey();
                        batch.InsertOrReplace(login);

                        var loginIndexItem = new UserLoginProviderKeyIndexEntity(user.Id, login.ProviderKey, login.LoginProvider);
                        loginIndexItems.Add(loginIndexItem);
                    }
                    await _userLoginTable.ExecuteBatchAsync(batch);

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