Esempio 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 login     = new TableUserLogin(user.Id, loginInfo.LoginProvider, loginInfo.ProviderKey);
            var operation = TableOperation.Insert(login);
            await _loginTable.ExecuteAsync(operation);

            var loginIndexItem = new TableUserLoginProviderKeyIndex(user.Id, login.ProviderKey, login.LoginProvider);
            await _loginProviderKeyIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(loginIndexItem));
        }
Esempio n. 2
0
        public async Task <T> FindAsync(UserLoginInfo login)
        {
            if (login == null)
            {
                throw new ArgumentNullException("login");
            }

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

            TableUserLoginProviderKeyIndex indexItem = (TableUserLoginProviderKeyIndex)loginProviderKeyIndexResult.Result;

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

            return(await FindByIdAsync(indexItem.UserId));
        }
Esempio 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");
            }

            TableUserLogin login = new TableUserLogin(user.Id, loginInfo.LoginProvider, loginInfo.ProviderKey);

            login.ETag = "*";
            TableOperation operation = TableOperation.Delete(login);
            await _loginTable.ExecuteAsync(operation);

            TableUserLoginProviderKeyIndex loginIndexItem = new TableUserLoginProviderKeyIndex(user.Id, login.ProviderKey, login.LoginProvider);

            loginIndexItem.ETag = "*";
            TableOperation indexOperation = TableOperation.Delete(loginIndexItem);
            await _loginProviderKeyIndexTable.ExecuteAsync(indexOperation);
        }
Esempio n. 4
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;
            }
        }
Esempio n. 5
0
        public async Task RemoveAllLoginsAsync(T user)
        {
            bool error = false;
            List <TableUserLogin> Logins                    = new List <TableUserLogin>();
            string partitionKeyQuery                        = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, user.Id);
            TableQuery <TableUserLogin>        query        = new TableQuery <TableUserLogin>().Where(partitionKeyQuery);
            TableQuerySegment <TableUserLogin> querySegment = null;

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

                Logins.AddRange(querySegment.Results);
            }

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

            foreach (TableUserLogin login in Logins)
            {
                login.ETag = "*"; //Delete even if it has changed
                batch.Add(TableOperation.Delete(login));
                TableUserLoginProviderKeyIndex providerKeyIndex = new TableUserLoginProviderKeyIndex(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 _loginTable.ExecuteBatchAsync(batch);

                        batch.Clear();
                    }
                    catch { }

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

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

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

                    batch.Clear();
                }
                catch { }

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

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

            if (error)
            {
                throw new Exception();
            }
        }