Beispiel #1
0
        public virtual string ProtectToken(SessionToken token, string purpose)
        {
            Requires.NotNull(token, "token");
            Requires.NotNullOrEmpty(purpose, "purpose");

            byte[] encoded = token.EncodeToken();
            return Protect(encoded, purpose);
        }
Beispiel #2
0
        public void FailsToUnprotectTokenIfPurposeIsDifferent()
        {
            // Arrange
            var tokens = CreateService();
            var expires = DateTime.UtcNow;
            var token = new SessionToken(
                new ReviewRPrincipal(
                    new ReviewRIdentity()
                    {
                        Email = "*****@*****.**",
                        DisplayName = "Swedish Chef",
                        Roles = new HashSet<string>()
                    }), expires);

            // Act
            string protectedToken = tokens.ProtectToken(token, "porpoise!");
            Assert.Throws<InvalidDataException>(() => tokens.UnprotectToken(protectedToken, "notporpoise??!"));
        }
Beispiel #3
0
        public void CorrectlyUnprotectsProtectedTokenWithSamePurpose()
        {
            // Arrange
            var tokens = CreateService();
            var expires = DateTime.UtcNow;
            var token = new SessionToken(
                new ReviewRPrincipal(
                    new ReviewRIdentity()
                    {
                        Email = "*****@*****.**",
                        DisplayName = "Swedish Chef",
                        Roles = new HashSet<string>()
                    }), expires);

            // Act
            string protectedToken = tokens.ProtectToken(token, "porpoise!");
            SessionToken unprotected = tokens.UnprotectToken(protectedToken, "porpoise!");

            // Assert
            Assert.Equal("Swedish Chef", unprotected.User.Identity.DisplayName);
            Assert.Equal("*****@*****.**", unprotected.User.Identity.Email);
            Assert.Equal(expires, unprotected.Expires);
        }
Beispiel #4
0
 public override string ProtectToken(SessionToken token, string purpose)
 {
     byte[] encoded = token.EncodeToken();
     return purpose + "|" + Convert.ToBase64String(encoded);
 }