public void CanUpdateTokens()
        {
            var props  = new AuthenticationProperties();
            var tokens = new List <AuthenticationToken>();
            var tok1   = new AuthenticationToken {
                Name = "One", Value = "1"
            };
            var tok2 = new AuthenticationToken {
                Name = "Two", Value = "2"
            };
            var tok3 = new AuthenticationToken {
                Name = "Three", Value = "3"
            };

            tokens.Add(tok1);
            tokens.Add(tok2);
            tokens.Add(tok3);
            props.StoreTokens(tokens);

            tok1.Value = ".1";
            tok2.Value = ".2";
            tok3.Value = ".3";
            props.StoreTokens(tokens);

            Assert.Equal(".1", props.GetTokenValue("One"));
            Assert.Equal(".2", props.GetTokenValue("Two"));
            Assert.Equal(".3", props.GetTokenValue("Three"));
            Assert.Equal(3, props.GetTokens().Count());
        }
        public void UpdateTokenValueReturnsFalseForUnknownToken()
        {
            var props  = new AuthenticationProperties();
            var tokens = new List <AuthenticationToken>();
            var tok1   = new AuthenticationToken {
                Name = "One", Value = "1"
            };
            var tok2 = new AuthenticationToken {
                Name = "Two", Value = "2"
            };
            var tok3 = new AuthenticationToken {
                Name = "Three", Value = "3"
            };

            tokens.Add(tok1);
            tokens.Add(tok2);
            tokens.Add(tok3);
            props.StoreTokens(tokens);

            Assert.False(props.UpdateTokenValue("ONE", ".11"));
            Assert.False(props.UpdateTokenValue("Jigglypuff", ".11"));

            Assert.Null(props.GetTokenValue("ONE"));
            Assert.Null(props.GetTokenValue("Jigglypuff"));
            Assert.Equal(3, props.GetTokens().Count());
        }
        public void SubsequentStoreTokenDeletesPreviousTokens()
        {
            var props  = new AuthenticationProperties();
            var tokens = new List <AuthenticationToken>();
            var tok1   = new AuthenticationToken {
                Name = "One", Value = "1"
            };
            var tok2 = new AuthenticationToken {
                Name = "Two", Value = "2"
            };
            var tok3 = new AuthenticationToken {
                Name = "Three", Value = "3"
            };

            tokens.Add(tok1);
            tokens.Add(tok2);
            tokens.Add(tok3);

            props.StoreTokens(tokens);

            props.StoreTokens(new[] { new AuthenticationToken {
                                          Name = "Zero", Value = "0"
                                      } });

            Assert.Equal("0", props.GetTokenValue("Zero"));
            Assert.Null(props.GetTokenValue("One"));
            Assert.Null(props.GetTokenValue("Two"));
            Assert.Null(props.GetTokenValue("Three"));
            Assert.Single(props.GetTokens());
        }
        /// <summary>
        /// Stores a set of authentication tokens, after removing any old tokens.
        /// </summary>
        /// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
        /// <param name="tokens">The tokens to store.</param>
        public static void StoreTokens(this AuthenticationProperties properties, IEnumerable <AuthenticationToken> tokens)
        {
            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            // Clear old tokens first
            var oldTokens = properties.GetTokens();

            foreach (var t in oldTokens)
            {
                properties.Items.Remove(TokenKeyPrefix + t.Name);
            }
            properties.Items.Remove(TokenNamesKey);

            var tokenNames = new List <string>();

            foreach (var token in tokens)
            {
                // REVIEW: should probably check that there are no ; in the token name and throw or encode
                tokenNames.Add(token.Name);
                properties.Items[TokenKeyPrefix + token.Name] = token.Value;
            }
            if (tokenNames.Count > 0)
            {
                properties.Items[TokenNamesKey] = string.Join(";", tokenNames.ToArray());
            }
        }