From a3f0ee3330e936cad13b4391a1f7a13ff26764fd Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Wed, 12 Aug 2015 14:10:42 -0700 Subject: [PATCH] Add a shared dataprotection test for cookies --- .../CookieAuthenticationMiddleware.cs | 3 +- .../Cookies/CookieMiddlewareTests.cs | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs index a0f98da50a..6765e4d815 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs @@ -32,8 +32,7 @@ namespace Microsoft.AspNet.Authentication.Cookies } if (Options.TicketDataFormat == null) { - var dataProtector = dataProtectionProvider.CreateProtector( - typeof(CookieAuthenticationMiddleware).FullName, Options.AuthenticationScheme, "v2"); + var dataProtector = dataProtectionProvider.CreateProtector(typeof(CookieAuthenticationMiddleware).FullName, Options.AuthenticationScheme, "v2"); Options.TicketDataFormat = new TicketDataFormat(dataProtector); } if (Options.CookieManager == null) diff --git a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs index 7136ce7f51..f438ff5802 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs @@ -13,11 +13,13 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.DataProtection; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Features.Authentication; using Microsoft.AspNet.TestHost; using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.Internal; using Shouldly; using Xunit; @@ -885,6 +887,66 @@ namespace Microsoft.AspNet.Authentication.Cookies location.LocalPath.ShouldBe("/base/denied"); } + [Fact] + public async Task CanSpecifyAndShareDataProtector() + { + + var dp = new NoOpDataProtector(); + var server1 = TestServer.Create(app => + { + app.UseCookieAuthentication(options => + { + options.TicketDataFormat = new TicketDataFormat(dp); + options.CookieName = "Cookie"; + }); + app.Use((context, next) => + context.Authentication.SignInAsync("Cookies", + new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))), + new AuthenticationProperties())); + }, + services => services.AddAuthentication()); + + var transaction = await SendAsync(server1, "http://example.com/stuff"); + transaction.SetCookie.ShouldNotBe(null); + + var server2 = TestServer.Create(app => + { + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = "Cookies"; + options.CookieName = "Cookie"; + options.TicketDataFormat = new TicketDataFormat(dp); + }); + app.Use(async (context, next) => + { + var authContext = new AuthenticateContext("Cookies"); + await context.Authentication.AuthenticateAsync(authContext); + Describe(context.Response, authContext); + }); + }, + services => services.AddAuthentication()); + var transaction2 = await SendAsync(server2, "http://example.com/stuff", transaction.CookieNameValue); + FindClaimValue(transaction2, ClaimTypes.Name).ShouldBe("Alice"); + } + + private class NoOpDataProtector : IDataProtector + { + public IDataProtector CreateProtector(string purpose) + { + return this; + } + + public byte[] Protect(byte[] plaintext) + { + return plaintext; + } + + public byte[] Unprotect(byte[] protectedData) + { + return protectedData; + } + } + private static string FindClaimValue(Transaction transaction, string claimType) { var claim = transaction.ResponseElement.Elements("claim").SingleOrDefault(elt => elt.Attribute("type").Value == claimType);