Add a shared dataprotection test for cookies

This commit is contained in:
Hao Kung 2015-08-12 14:10:42 -07:00
parent 16914ec021
commit a3f0ee3330
2 changed files with 63 additions and 2 deletions

View File

@ -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)

View File

@ -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);