Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ protected override async Task HandleSignOutAsync(AuthenticationProperties? prope
if (Options.SessionStore != null && _sessionKey != null)
{
await Options.SessionStore.RemoveAsync(_sessionKey, Context, Context.RequestAborted);

// Clear the cached session key so a subsequent SignInAsync call in the same request
// generates a new key via StoreAsync rather than reusing the just-removed key via RenewAsync.
// See https://github.com/dotnet/aspnetcore/issues/47503.
_sessionKey = null;
}

var context = new CookieSigningOutContext(
Expand Down
65 changes: 65 additions & 0 deletions src/Security/Authentication/test/CookieTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,71 @@ public async Task SessionStoreRemovesExpired()
Assert.Null(FindClaimValue(transaction3, ClaimTypes.Name));
}

[Fact]
public async Task SignOutClearsSessionKeySoSubsequentSignInGeneratesNewKey()
{
var sessionStore = new TestTicketStore();
using var host = await CreateHostWithServices(s =>
{
s.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o =>
{
o.TimeProvider = _timeProvider;
o.SessionStore = sessionStore;
});
}, async context =>
{
if (context.Request.Query.ContainsKey("signoutfirst"))
{
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

// Sign back in as a different principal to mirror the privilege-rotation
// scenario from https://github.com/dotnet/aspnetcore/issues/47503.
var bob = new ClaimsIdentity(new GenericIdentity("Bob", "Cookies"));
await context.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(bob),
new AuthenticationProperties());
}
else
{
await SignInAsAlice(context);
}
});

using var server = host.GetTestServer();

// Establish an initial session as Alice with one key in the store.
var transaction1 = await SendAsync(server, "http://example.com/testpath");
var key1 = Assert.Single(sessionStore.Store.Keys);

// In the same request, sign out and then sign back in as Bob while attaching the
// existing cookie. SignOutAsync must clear the cached session key so that the
// subsequent SignInAsync generates a fresh key via StoreAsync rather than reusing
// the just-removed key via RenewAsync.
// See https://github.com/dotnet/aspnetcore/issues/47503.
//
// Same-request SignOut + SignIn emits two Set-Cookie headers (the sign-out
// delete-cookie followed by the sign-in auth-cookie), so the shared SendAsync
// helper's SingleOrDefault on Set-Cookie can't be used here. Extract the
// auth-cookie (the last Set-Cookie value) manually.
var request2 = new HttpRequestMessage(HttpMethod.Get, "http://example.com/testpath?signoutfirst=1");
request2.Headers.Add("Cookie", transaction1.CookieNameValue);
using var response2 = await server.CreateClient().SendAsync(request2);
Assert.Equal(HttpStatusCode.OK, response2.StatusCode);

var key2 = Assert.Single(sessionStore.Store.Keys);
Assert.NotEqual(key1, key2);

var setCookies = response2.Headers.GetValues("Set-Cookie").ToArray();
Assert.Equal(2, setCookies.Length);
var authCookieNameValue = setCookies[^1].Split(';', 2)[0];

// The new cookie must actually authenticate, and it must resolve to Bob rather
// than the pre-signout Alice identity.
var transaction3 = await SendAsync(server, "http://example.com/me/Cookies", authCookieNameValue);
Assert.Equal("Bob", FindClaimValue(transaction3, ClaimTypes.Name));
}

[Fact]
public async Task CustomAuthSchemeEncodesCookieName()
{
Expand Down
Loading