Ejemplo n.º 1
0
        public async Task AddLoginAsync(T user, UserLoginInfo loginInfo)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (loginInfo == null)
            {
                throw new ArgumentNullException("loginInfo");
            }

            var userLogin = new UserLoginEntity(user.Id, loginInfo.LoginProvider, loginInfo.ProviderKey);
            await _userLoginTable.ExecuteAsync(TableOperation.Insert(userLogin));

            var userLoginProviderKeyIndex = new UserLoginProviderKeyIndexEntity(user.Id, userLogin.ProviderKey, userLogin.LoginProvider);
            await _userLoginProviderKeyIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(userLoginProviderKeyIndex));
        }
Ejemplo n.º 2
0
        public async Task <T> FindAsync(UserLoginInfo login)
        {
            if (login == null)
            {
                throw new ArgumentNullException("login");
            }

            var         candidateIndex = new UserLoginProviderKeyIndexEntity("", login.ProviderKey, login.LoginProvider);
            TableResult loginProviderKeyIndexResult =
                await
                _userLoginProviderKeyIndexTable.ExecuteAsync(
                    TableOperation.Retrieve <UserLoginProviderKeyIndexEntity>(candidateIndex.PartitionKey, ""));

            var indexItem = (UserLoginProviderKeyIndexEntity)loginProviderKeyIndexResult.Result;

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

            return(await FindByIdAsync(indexItem.UserId));
        }
Ejemplo n.º 3
0
        public async Task RemoveLoginAsync(T user, UserLoginInfo loginInfo)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (loginInfo == null)
            {
                throw new ArgumentNullException("loginInfo");
            }

            var userLogin = new UserLoginEntity(user.Id, loginInfo.LoginProvider, loginInfo.ProviderKey)
            {
                ETag = "*"
            };
            await _userLoginTable.ExecuteAsync(TableOperation.Delete(userLogin));

            var userLoginProviderKeyIndex = new UserLoginProviderKeyIndexEntity(user.Id, userLogin.ProviderKey, userLogin.LoginProvider)
            {
                ETag = "*"
            };
            await _userLoginProviderKeyIndexTable.ExecuteAsync(TableOperation.Delete(userLoginProviderKeyIndex));
        }
Ejemplo n.º 4
0
        public async Task RemoveAllLoginsAsync(T user)
        {
            bool   error             = false;
            var    Logins            = new List <UserLoginEntity>();
            string partitionKeyQuery = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, user.Id);
            TableQuery <UserLoginEntity>        query        = new TableQuery <UserLoginEntity>().Where(partitionKeyQuery);
            TableQuerySegment <UserLoginEntity> querySegment = null;

            while (querySegment == null || querySegment.ContinuationToken != null)
            {
                querySegment =
                    await _userLoginTable.ExecuteQuerySegmentedAsync(query, querySegment != null?querySegment.ContinuationToken : null);

                Logins.AddRange(querySegment.Results);
            }

            var batch      = new TableBatchOperation();
            var batchIndex = new TableBatchOperation();

            foreach (UserLoginEntity login in Logins)
            {
                login.ETag = "*"; //Delete even if it has changed
                batch.Add(TableOperation.Delete(login));
                var providerKeyIndex = new UserLoginProviderKeyIndexEntity(user.Id, login.ProviderKey, login.LoginProvider);
                providerKeyIndex.ETag = "*";
                batchIndex.Add(TableOperation.Delete(providerKeyIndex));

                if (batch.Count >= 100 || batchIndex.Count >= 100)
                {
                    try
                    {
                        //Try executing as a batch
                        await _userLoginTable.ExecuteBatchAsync(batch);

                        batch.Clear();
                    }
                    catch
                    {
                    }

                    //If a batch wont work, try individually
                    foreach (TableOperation op in batch)
                    {
                        try
                        {
                            await _userLoginTable.ExecuteAsync(op);
                        }
                        catch
                        {
                            error = true;
                        }
                    }

                    //Delete the index individually becase of the partition keys
                    foreach (TableOperation op in batchIndex)
                    {
                        try
                        {
                            await _userLoginProviderKeyIndexTable.ExecuteAsync(op);
                        }
                        catch
                        {
                            error = true;
                        }
                    }

                    batch.Clear();
                    batchIndex.Clear();
                }
            }
            if (batch.Count > 0 || batchIndex.Count > 0)
            {
                try
                {
                    //Try executing as a batch
                    await _userLoginTable.ExecuteBatchAsync(batch);

                    batch.Clear();
                }
                catch
                {
                }

                //If a batch wont work, try individually
                foreach (TableOperation op in batch)
                {
                    try
                    {
                        await _userLoginTable.ExecuteAsync(op);
                    }
                    catch
                    {
                        error = true;
                    }
                }

                //Delete the index individually becase of the partition keys
                foreach (TableOperation op in batchIndex)
                {
                    try
                    {
                        await _userLoginProviderKeyIndexTable.ExecuteAsync(op);
                    }
                    catch
                    {
                        error = true;
                    }
                }
            }

            if (error)
            {
                throw new Exception();
            }
        }
Ejemplo n.º 5
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;
            }
        }