diff --git a/src/Microsoft.AspNet.Authentication/DataHandler/TicketSerializer.cs b/src/Microsoft.AspNet.Authentication/DataHandler/TicketSerializer.cs index 310b2b03a8..44284d788f 100644 --- a/src/Microsoft.AspNet.Authentication/DataHandler/TicketSerializer.cs +++ b/src/Microsoft.AspNet.Authentication/DataHandler/TicketSerializer.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Authentication { public class TicketSerializer : IDataSerializer { - private const int FormatVersion = 3; + private const int FormatVersion = 4; public virtual byte[] Serialize(AuthenticationTicket model) { @@ -63,6 +63,17 @@ namespace Microsoft.AspNet.Authentication WriteWithDefault(writer, claim.Issuer, DefaultValues.LocalAuthority); WriteWithDefault(writer, claim.OriginalIssuer, claim.Issuer); } + + var bootstrap = identity.BootstrapContext as string; + if (string.IsNullOrEmpty(bootstrap)) + { + writer.Write(0); + } + else + { + writer.Write(bootstrap.Length); + writer.Write(bootstrap); + } } } PropertiesSerializer.Write(writer, model.Properties); @@ -99,7 +110,13 @@ namespace Microsoft.AspNet.Authentication var originalIssuer = ReadWithDefault(reader, issuer); claims[index] = new Claim(type, value, valueType, issuer, originalIssuer); } - identities[i] = new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType); + var identity = new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType); + var bootstrapContextSize = reader.ReadInt32(); + if (bootstrapContextSize > 0) + { + identity.BootstrapContext = reader.ReadString(); + } + identities[i] = identity; } var properties = PropertiesSerializer.Read(reader); diff --git a/test/Microsoft.AspNet.Authentication.Test/DataHandler/TicketSerializerTests.cs b/test/Microsoft.AspNet.Authentication.Test/DataHandler/TicketSerializerTests.cs index b1eaa07095..23c7add5a9 100644 --- a/test/Microsoft.AspNet.Authentication.Test/DataHandler/TicketSerializerTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/DataHandler/TicketSerializerTests.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Security.Claims; using Microsoft.AspNet.Http.Authentication; -using Shouldly; using Xunit; namespace Microsoft.AspNet.Authentication @@ -42,9 +41,30 @@ namespace Microsoft.AspNet.Authentication TicketSerializer.Write(writer, ticket); stream.Position = 0; var readTicket = TicketSerializer.Read(reader); - readTicket.Principal.Identities.Count().ShouldBe(0); - readTicket.Properties.RedirectUri.ShouldBe("bye"); - readTicket.AuthenticationScheme.ShouldBe("Hello"); + Assert.Equal(0, readTicket.Principal.Identities.Count()); + Assert.Equal("bye", readTicket.Properties.RedirectUri); + Assert.Equal("Hello", readTicket.AuthenticationScheme); + } + } + + [Fact] + public void CanRoundTripBootstrapContext() + { + var properties = new AuthenticationProperties(); + properties.RedirectUri = "bye"; + var ticket = new AuthenticationTicket(new ClaimsPrincipal(), properties, "Hello"); + ticket.Principal.AddIdentity(new ClaimsIdentity("misc") { BootstrapContext = "bootstrap" }); + + using (var stream = new MemoryStream()) + using (var writer = new BinaryWriter(stream)) + using (var reader = new BinaryReader(stream)) + { + TicketSerializer.Write(writer, ticket); + stream.Position = 0; + var readTicket = TicketSerializer.Read(reader); + Assert.Equal(1, readTicket.Principal.Identities.Count()); + Assert.Equal("misc", readTicket.Principal.Identity.AuthenticationType); + Assert.Equal("bootstrap", readTicket.Principal.Identities.First().BootstrapContext); } } }