Skip to content

Commit 6241625

Browse files
authored
Combine email and username in MapIdentityApi (#49981)
1 parent 7a170ea commit 6241625

File tree

10 files changed

+382
-387
lines changed

10 files changed

+382
-387
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Identity.DTO;
5+
6+
internal sealed class ForgotPasswordRequest
7+
{
8+
public required string Email { get; init; }
9+
}

src/Identity/Core/src/DTO/InfoRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace Microsoft.AspNetCore.Identity.DTO;
55

66
internal sealed class InfoRequest
77
{
8-
public string? NewUsername { get; init; }
98
public string? NewEmail { get; init; }
109
public string? NewPassword { get; init; }
1110
public string? OldPassword { get; init; }

src/Identity/Core/src/DTO/InfoResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.AspNetCore.Identity.DTO;
55

66
internal sealed class InfoResponse
77
{
8-
public required string Username { get; init; }
98
public required string Email { get; init; }
9+
public required bool IsEmailConfirmed { get; init; }
1010
public required IDictionary<string, string> Claims { get; init; }
1111
}

src/Identity/Core/src/DTO/LoginRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.AspNetCore.Identity.DTO;
55

66
internal sealed class LoginRequest
77
{
8-
public required string Username { get; init; }
8+
public required string Email { get; init; }
99
public required string Password { get; init; }
1010
public string? TwoFactorCode { get; init; }
1111
public string? TwoFactorRecoveryCode { get; init; }

src/Identity/Core/src/DTO/RegisterRequest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace Microsoft.AspNetCore.Identity.DTO;
55

66
internal sealed class RegisterRequest
77
{
8-
public required string Username { get; init; }
9-
public required string Password { get; init; }
108
public required string Email { get; init; }
9+
public required string Password { get; init; }
1110
}

src/Identity/Core/src/DTO/ResetPasswordRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace Microsoft.AspNetCore.Identity.DTO;
66
internal sealed class ResetPasswordRequest
77
{
88
public required string Email { get; init; }
9-
public string? ResetCode { get; init; }
10-
public string? NewPassword { get; init; }
9+
public required string ResetCode { get; init; }
10+
public required string NewPassword { get; init; }
1111
}

src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs

Lines changed: 111 additions & 164 deletions
Large diffs are not rendered by default.

src/Identity/test/Identity.FunctionalTests/MapIdentityApiTests.cs

Lines changed: 254 additions & 193 deletions
Large diffs are not rendered by default.

src/Security/Authentication/BearerToken/src/BearerTokenHandler.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ namespace Microsoft.AspNetCore.Authentication.BearerToken;
1414
internal sealed class BearerTokenHandler(IOptionsMonitor<BearerTokenOptions> optionsMonitor, ILoggerFactory loggerFactory, UrlEncoder urlEncoder)
1515
: SignInAuthenticationHandler<BearerTokenOptions>(optionsMonitor, loggerFactory, urlEncoder)
1616
{
17-
private static readonly long OneSecondTicks = TimeSpan.FromSeconds(1).Ticks;
18-
1917
private static readonly AuthenticateResult FailedUnprotectingToken = AuthenticateResult.Fail("Unprotected token failed");
2018
private static readonly AuthenticateResult TokenExpired = AuthenticateResult.Fail("Token expired");
2119

2220
private new BearerTokenEvents Events => (BearerTokenEvents)base.Events!;
2321

2422
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
2523
{
26-
// Give application opportunity to find from a different location, adjust, or reject token
24+
// Give application opportunity to find from a different location, adjust, or reject token.
2725
var messageReceivedContext = new MessageReceivedContext(Context, Scheme, Options);
2826

2927
await Events.MessageReceivedAsync(messageReceivedContext);
@@ -66,12 +64,12 @@ protected override async Task HandleSignInAsync(ClaimsPrincipal user, Authentica
6664
var utcNow = TimeProvider.GetUtcNow();
6765

6866
properties ??= new();
69-
properties.ExpiresUtc ??= utcNow + Options.BearerTokenExpiration;
67+
properties.ExpiresUtc = utcNow + Options.BearerTokenExpiration;
7068

7169
var response = new AccessTokenResponse
7270
{
7371
AccessToken = Options.BearerTokenProtector.Protect(CreateBearerTicket(user, properties)),
74-
ExpiresInSeconds = CalculateExpiresInSeconds(utcNow, properties.ExpiresUtc),
72+
ExpiresInSeconds = (long)Options.BearerTokenExpiration.TotalSeconds,
7573
RefreshToken = Options.RefreshTokenProtector.Protect(CreateRefreshTicket(user, utcNow)),
7674
};
7775

@@ -92,24 +90,6 @@ protected override async Task HandleSignInAsync(ClaimsPrincipal user, Authentica
9290
: null;
9391
}
9492

95-
private long CalculateExpiresInSeconds(DateTimeOffset utcNow, DateTimeOffset? expiresUtc)
96-
{
97-
static DateTimeOffset FloorSeconds(DateTimeOffset dateTimeOffset)
98-
=> new(dateTimeOffset.Ticks / OneSecondTicks * OneSecondTicks, dateTimeOffset.Offset);
99-
100-
// AuthenticationProperties floors ExpiresUtc. If this remains unchanged, we'll use BearerTokenExpiration directly
101-
// to produce a consistent ExpiresInTotalSeconds values. If ExpiresUtc was overridden, we just calculate the
102-
// the difference from utcNow and round even though this will likely result in unstable values.
103-
var expiresTimeSpan = Options.BearerTokenExpiration;
104-
var expectedExpiresUtc = FloorSeconds(utcNow + expiresTimeSpan);
105-
return (long)(expiresUtc switch
106-
{
107-
DateTimeOffset d when d == expectedExpiresUtc => expiresTimeSpan.TotalSeconds,
108-
DateTimeOffset d => (d - utcNow).TotalSeconds,
109-
_ => expiresTimeSpan.TotalSeconds,
110-
});
111-
}
112-
11393
private AuthenticationTicket CreateBearerTicket(ClaimsPrincipal user, AuthenticationProperties properties)
11494
=> new(user, properties, $"{Scheme.Name}:AccessToken");
11595

0 commit comments

Comments
 (0)