Azure tables forbid the folowing characters in PartitionKey and RowKey properties: --The forward slash(/) character --The backslash(\) character --The number sign(#) character --The question mark (?) character --Control characters from U+0000 to U+001F, including: --The horizontal tab(\t) character --The linefeed(\n) character --The carriage return (\r) character --Control characters from U+007F to U+009F (see https://msdn.microsoft.com/library/azure/dd179338.aspx) This class provides a method to encode any string to remove the forbidden characters and allow its use as a PartitionKey or RowKey, as well as a method to decode the keys to recover the original string with the forbidden characters. It encodes forbidden chracters using the '!' (exclamation point) character as an escape character. !! = ! ('!' is a legal character but '!!' is needed to indicate '!' is not escaping anything) !f = / !b = \ !p = # !q = ? !t = \t (tab) !n = \n (line feed) !r = \r (carriage return) !xXY = character with unicode value 0xXY
コード例 #1
0
        public override async Task <bool> AddIncorrectPhaseTwoHashAsync(
            DbUserAccount userAccount,
            string phase2Hash, DateTime?whenSeenUtc = null,
            CancellationToken cancellationToken     = default(CancellationToken))
        {
            // Retrieve a reference to the table.
            CloudTable table = await GetTableAsync(TableName_RecentIncorrectPhase2Hashes, cancellationToken);

            TableOperation retriveOperation =
                TableOperation.Retrieve <IncorrectPhaseTwoHashEntity>(TableKeyEncoding.Encode(phase2Hash),
                                                                      TableKeyEncoding.Encode(userAccount.UsernameOrAccountId));

            TableResult retrievedResult = await table.ExecuteAsync(retriveOperation, getTableRequestOptions(200), getOperationContext(), cancellationToken);

            if (retrievedResult.Result != null)
            {
                return(true);
            }

            // Write the hash to azure tables in the background.
            TaskHelper.RunInBackground(
                table.ExecuteAsync(
                    TableOperation.InsertOrReplace(new IncorrectPhaseTwoHashEntity(userAccount.UsernameOrAccountId, phase2Hash, whenSeenUtc)),
                    getTableRequestOptions(), getOperationContext(), cancellationToken)
                );

            return(false);
        }
コード例 #2
0
        public override async Task <bool> HasClientWithThisHashedCookieSuccessfullyLoggedInBeforeAsync(
            DbUserAccount userAccount,
            string hashOfCookie,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Retrieve a reference to the table.
            CloudTable table = await GetTableAsync(TableName_SuccessfulLoginCookie, cancellationToken);

            //TableQuery<IncorrectPhaseTwoHashEntity> query = new TableQuery<IncorrectPhaseTwoHashEntity>().Where(TableQuery.CombineFilters(
            //        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, TableKeyEncoding.Encode(hashOfCookie)),
            //        TableOperators.And,
            //        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, TableKeyEncoding.Encode(UsernameOrAccountId))));

            var query =
                (from e in table.CreateQuery <SuccessfulLoginCookieEntity>()
                 where e.PartitionKey == TableKeyEncoding.Encode(hashOfCookie) &&
                 e.RowKey == TableKeyEncoding.Encode(userAccount.UsernameOrAccountId)
                 select e).AsTableQuery();

            int count = 0;

            TableContinuationToken continuationToken = null;

            do
            {
                var partialResult = await query.ExecuteSegmentedAsync(continuationToken, cancellationToken);

                count            += partialResult.Results.Count;
                continuationToken = partialResult.ContinuationToken;
            } while (continuationToken != null);

            return(count > 0);
        }
コード例 #3
0
        public override async Task <bool> HasClientWithThisHashedCookieSuccessfullyLoggedInBeforeAsync(
            DbUserAccount userAccount,
            string hashOfCookie,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Retrieve a reference to the table.
            CloudTable table = await GetTableAsync(TableName_SuccessfulLoginCookie, cancellationToken);

            TableOperation retriveOperation =
                TableOperation.Retrieve <SuccessfulLoginCookieEntity>(TableKeyEncoding.Encode(hashOfCookie),
                                                                      TableKeyEncoding.Encode(userAccount.UsernameOrAccountId));

            TableResult retrievedResult = await table.ExecuteAsync(retriveOperation, getTableRequestOptions(200), getOperationContext(), cancellationToken);

            return(retrievedResult.Result != null);
        }
コード例 #4
0
        public override async Task <bool> AddIncorrectPhaseTwoHashAsync(
            DbUserAccount userAccount,
            string phase2Hash, DateTime?whenSeenUtc = null,
            CancellationToken cancellationToken     = default(CancellationToken))
        {
            // Retrieve a reference to the table.
            CloudTable table = await GetTableAsync(TableName_RecentIncorrectPhase2Hashes, cancellationToken);

            //TableQuery<IncorrectPhaseTwoHashEntity> query = new TableQuery<IncorrectPhaseTwoHashEntity>().Where(TableQuery.CombineFilters(
            //        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, TableKeyEncoding.Encode(phase2Hash)),
            //        TableOperators.And,
            //        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, TableKeyEncoding.Encode(UsernameOrAccountId)) ));

            var query =
                (from e in table.CreateQuery <IncorrectPhaseTwoHashEntity>()
                 where e.PartitionKey == TableKeyEncoding.Encode(phase2Hash) &&
                 e.RowKey == TableKeyEncoding.Encode(userAccount.UsernameOrAccountId)
                 select e).AsTableQuery();

            int count = 0;

            TableContinuationToken continuationToken = null;

            do
            {
                var partialResult = await query.ExecuteSegmentedAsync(continuationToken, cancellationToken);

                count            += partialResult.Results.Count;
                continuationToken = partialResult.ContinuationToken;
            } while (continuationToken != null);

            if (count > 0)
            {
                // This phase2 hash already exists.
                return(true);
            }

            // Write the hash to azure tables in the background.
            TaskHelper.RunInBackground(
                table.ExecuteAsync(
                    TableOperation.InsertOrReplace(new IncorrectPhaseTwoHashEntity(userAccount.UsernameOrAccountId, phase2Hash, whenSeenUtc)),
                    cancellationToken)
                );

            return(false);
        }
コード例 #5
0
 public SuccessfulLoginCookieEntity(string usernameOrAccountId, string hashOfCookie, DateTime?lastSeenUtc = null)
 {
     PartitionKey = TableKeyEncoding.Encode(hashOfCookie);
     RowKey       = TableKeyEncoding.Encode(usernameOrAccountId);
     LastSeenUtc  = lastSeenUtc ?? DateTime.UtcNow;
 }
コード例 #6
0
 public IncorrectPhaseTwoHashEntity(string usernameOrAccountId, string hashValue, DateTime?lastSeenUtc = null)
 {
     PartitionKey = TableKeyEncoding.Encode(hashValue);
     RowKey       = TableKeyEncoding.Encode(usernameOrAccountId);
     LastSeenUtc  = lastSeenUtc ?? DateTime.UtcNow;
 }