Skip to content

Commit 84c0baa

Browse files
authored
Add nullability to Identity / Identity.UI (#40167)
Contributes to #5680
1 parent d75c220 commit 84c0baa

File tree

74 files changed

+1425
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1425
-319
lines changed

src/Identity/Core/src/DataProtectorTokenProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public virtual async Task<string> GenerateAsync(string purpose, UserManager<TUse
8989
writer.Write(DateTimeOffset.UtcNow);
9090
writer.Write(userId);
9191
writer.Write(purpose ?? "");
92-
string stamp = null;
92+
string? stamp = null;
9393
if (manager.SupportsUserSecurityStamp)
9494
{
9595
stamp = await manager.GetSecurityStampAsync(user);

src/Identity/Core/src/ExternalLoginInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public ExternalLoginInfo(ClaimsPrincipal principal, string loginProvider, string
3333
/// <summary>
3434
/// The <see cref="AuthenticationToken"/>s associated with this login.
3535
/// </summary>
36-
public IEnumerable<AuthenticationToken> AuthenticationTokens { get; set; }
36+
public IEnumerable<AuthenticationToken>? AuthenticationTokens { get; set; }
3737

3838
/// <summary>
3939
/// The <see cref="Authentication.AuthenticationProperties"/> associated with this login.
4040
/// </summary>
41-
public AuthenticationProperties AuthenticationProperties { get; set; }
41+
public AuthenticationProperties? AuthenticationProperties { get; set; }
4242
}

src/Identity/Core/src/IdentityCookiesBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ public class IdentityCookiesBuilder
1414
/// <summary>
1515
/// Used to configure the application cookie.
1616
/// </summary>
17-
public OptionsBuilder<CookieAuthenticationOptions> ApplicationCookie { get; set; }
17+
public OptionsBuilder<CookieAuthenticationOptions>? ApplicationCookie { get; set; }
1818

1919
/// <summary>
2020
/// Used to configure the external cookie.
2121
/// </summary>
22-
public OptionsBuilder<CookieAuthenticationOptions> ExternalCookie { get; set; }
22+
public OptionsBuilder<CookieAuthenticationOptions>? ExternalCookie { get; set; }
2323

2424
/// <summary>
2525
/// Used to configure the two factor remember me cookie.
2626
/// </summary>
27-
public OptionsBuilder<CookieAuthenticationOptions> TwoFactorRememberMeCookie { get; set; }
27+
public OptionsBuilder<CookieAuthenticationOptions>? TwoFactorRememberMeCookie { get; set; }
2828

2929
/// <summary>
3030
/// Used to configure the two factor user id cookie.
3131
/// </summary>
32-
public OptionsBuilder<CookieAuthenticationOptions> TwoFactorUserIdCookie { get; set; }
32+
public OptionsBuilder<CookieAuthenticationOptions>? TwoFactorUserIdCookie { get; set; }
3333
}

src/Identity/Core/src/IdentityServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static IdentityBuilder AddIdentity<TUser, TRole>(
2424
this IServiceCollection services)
2525
where TUser : class
2626
where TRole : class
27-
=> services.AddIdentity<TUser, TRole>(setupAction: null);
27+
=> services.AddIdentity<TUser, TRole>(setupAction: null!);
2828

2929
/// <summary>
3030
/// Adds and configures the identity system for the specified User and Role types.

src/Identity/Core/src/LoggingExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable disable
5+
46
namespace Microsoft.Extensions.Logging;
57

68
internal static class LoggingExtensions

src/Identity/Core/src/Microsoft.AspNetCore.Identity.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageTags>aspnetcore;identity;membership</PackageTags>
99
<IsPackable>false</IsPackable>
10-
<Nullable>disable</Nullable>
1110
</PropertyGroup>
1211

1312
<ItemGroup>

src/Identity/Core/src/PublicAPI.Unshipped.txt

Lines changed: 219 additions & 0 deletions
Large diffs are not rendered by default.

src/Identity/Core/src/SecurityStampRefreshingPrincipalContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class SecurityStampRefreshingPrincipalContext
1313
/// <summary>
1414
/// The principal contained in the current cookie.
1515
/// </summary>
16-
public ClaimsPrincipal CurrentPrincipal { get; set; }
16+
public ClaimsPrincipal? CurrentPrincipal { get; set; }
1717

1818
/// <summary>
1919
/// The new principal which should replace the current.
2020
/// </summary>
21-
public ClaimsPrincipal NewPrincipal { get; set; }
21+
public ClaimsPrincipal? NewPrincipal { get; set; }
2222
}

src/Identity/Core/src/SecurityStampValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SecurityStampValidator(IOptions<SecurityStampValidatorOptions> options, S
3636
SignInManager = signInManager;
3737
Options = options.Value;
3838
Clock = clock;
39-
Logger = logger.CreateLogger(this.GetType().FullName);
39+
Logger = logger.CreateLogger(GetType());
4040
}
4141

4242
/// <summary>
@@ -102,7 +102,7 @@ protected virtual async Task SecurityStampVerified(TUser user, CookieValidatePri
102102
/// </summary>
103103
/// <param name="principal">The principal to verify.</param>
104104
/// <returns>The verified user or null if verification fails.</returns>
105-
protected virtual Task<TUser> VerifySecurityStamp(ClaimsPrincipal principal)
105+
protected virtual Task<TUser?> VerifySecurityStamp(ClaimsPrincipal? principal)
106106
=> SignInManager.ValidateSecurityStampAsync(principal);
107107

108108
/// <summary>

src/Identity/Core/src/SecurityStampValidatorOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public class SecurityStampValidatorOptions
1919
/// <summary>
2020
/// Invoked when the default security stamp validator replaces the user's ClaimsPrincipal in the cookie.
2121
/// </summary>
22-
public Func<SecurityStampRefreshingPrincipalContext, Task> OnRefreshingPrincipal { get; set; }
22+
public Func<SecurityStampRefreshingPrincipalContext, Task>? OnRefreshingPrincipal { get; set; }
2323
}

src/Identity/Core/src/SignInManager.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public SignInManager(UserManager<TUser> userManager,
6161
}
6262

6363
private readonly IHttpContextAccessor _contextAccessor;
64-
private HttpContext _context;
6564
private readonly IAuthenticationSchemeProvider _schemes;
6665
private readonly IUserConfirmation<TUser> _confirmation;
66+
private HttpContext? _context;
6767

6868
/// <summary>
6969
/// Gets the <see cref="ILogger"/> used to log messages from the manager.
@@ -196,7 +196,7 @@ public virtual async Task RefreshSignInAsync(TUser user)
196196
/// <param name="authenticationMethod">Name of the method used to authenticate the user.</param>
197197
/// <returns>The task object representing the asynchronous operation.</returns>
198198
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Required for backwards compatibility")]
199-
public virtual Task SignInAsync(TUser user, bool isPersistent, string authenticationMethod = null)
199+
public virtual Task SignInAsync(TUser user, bool isPersistent, string? authenticationMethod = null)
200200
=> SignInAsync(user, new AuthenticationProperties { IsPersistent = isPersistent }, authenticationMethod);
201201

202202
/// <summary>
@@ -207,7 +207,7 @@ public virtual Task SignInAsync(TUser user, bool isPersistent, string authentica
207207
/// <param name="authenticationMethod">Name of the method used to authenticate the user.</param>
208208
/// <returns>The task object representing the asynchronous operation.</returns>
209209
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Required for backwards compatibility")]
210-
public virtual Task SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string authenticationMethod = null)
210+
public virtual Task SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string? authenticationMethod = null)
211211
{
212212
IList<Claim> additionalClaims = Array.Empty<Claim>();
213213
if (authenticationMethod != null)
@@ -235,7 +235,7 @@ public virtual Task SignInWithClaimsAsync(TUser user, bool isPersistent, IEnumer
235235
/// <param name="authenticationProperties">Properties applied to the login and authentication cookie.</param>
236236
/// <param name="additionalClaims">Additional claims that will be stored in the cookie.</param>
237237
/// <returns>The task object representing the asynchronous operation.</returns>
238-
public virtual async Task SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable<Claim> additionalClaims)
238+
public virtual async Task SignInWithClaimsAsync(TUser user, AuthenticationProperties? authenticationProperties, IEnumerable<Claim> additionalClaims)
239239
{
240240
var userPrincipal = await CreateUserPrincipalAsync(user);
241241
foreach (var claim in additionalClaims)
@@ -264,7 +264,7 @@ public virtual async Task SignOutAsync()
264264
/// <param name="principal">The principal whose stamp should be validated.</param>
265265
/// <returns>The task object representing the asynchronous operation. The task will contain the <typeparamref name="TUser"/>
266266
/// if the stamp matches the persisted value, otherwise it will return null.</returns>
267-
public virtual async Task<TUser> ValidateSecurityStampAsync(ClaimsPrincipal principal)
267+
public virtual async Task<TUser?> ValidateSecurityStampAsync(ClaimsPrincipal? principal)
268268
{
269269
if (principal == null)
270270
{
@@ -287,7 +287,7 @@ public virtual async Task<TUser> ValidateSecurityStampAsync(ClaimsPrincipal prin
287287
/// <param name="principal">The principal whose stamp should be validated.</param>
288288
/// <returns>The task object representing the asynchronous operation. The task will contain the <typeparamref name="TUser"/>
289289
/// if the stamp matches the persisted value, otherwise it will return null.</returns>
290-
public virtual async Task<TUser> ValidateTwoFactorSecurityStampAsync(ClaimsPrincipal principal)
290+
public virtual async Task<TUser?> ValidateTwoFactorSecurityStampAsync(ClaimsPrincipal? principal)
291291
{
292292
if (principal == null || principal.Identity?.Name == null)
293293
{
@@ -309,7 +309,7 @@ public virtual async Task<TUser> ValidateTwoFactorSecurityStampAsync(ClaimsPrinc
309309
/// <param name="user">The user whose stamp should be validated.</param>
310310
/// <param name="securityStamp">The expected security stamp value.</param>
311311
/// <returns>The result of the validation.</returns>
312-
public virtual async Task<bool> ValidateSecurityStampAsync(TUser user, string securityStamp)
312+
public virtual async Task<bool> ValidateSecurityStampAsync(TUser? user, string? securityStamp)
313313
=> user != null &&
314314
// Only validate the security stamp if the store supports it
315315
(!UserManager.SupportsUserSecurityStamp || securityStamp == await UserManager.GetSecurityStampAsync(user));
@@ -585,15 +585,15 @@ public virtual async Task<SignInResult> TwoFactorSignInAsync(string provider, st
585585
/// </summary>
586586
/// <returns>The task object representing the asynchronous operation containing the <typeparamref name="TUser"/>
587587
/// for the sign-in attempt.</returns>
588-
public virtual async Task<TUser> GetTwoFactorAuthenticationUserAsync()
588+
public virtual async Task<TUser?> GetTwoFactorAuthenticationUserAsync()
589589
{
590590
var info = await RetrieveTwoFactorInfoAsync();
591591
if (info == null)
592592
{
593593
return null;
594594
}
595595

596-
return await UserManager.FindByIdAsync(info.UserId);
596+
return await UserManager.FindByIdAsync(info.UserId!);
597597
}
598598

599599
/// <summary>
@@ -648,7 +648,7 @@ public virtual async Task<IEnumerable<AuthenticationScheme>> GetExternalAuthenti
648648
/// <param name="expectedXsrf">Flag indication whether a Cross Site Request Forgery token was expected in the current request.</param>
649649
/// <returns>The task object representing the asynchronous operation containing the <see name="ExternalLoginInfo"/>
650650
/// for the sign-in attempt.</returns>
651-
public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string expectedXsrf = null)
651+
public virtual async Task<ExternalLoginInfo?> GetExternalLoginInfoAsync(string? expectedXsrf = null)
652652
{
653653
var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme);
654654
var items = auth?.Properties?.Items;
@@ -681,7 +681,7 @@ public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string ex
681681
?? provider;
682682
return new ExternalLoginInfo(auth.Principal, provider, providerKey, providerDisplayName)
683683
{
684-
AuthenticationTokens = auth.Properties.GetTokens(),
684+
AuthenticationTokens = auth.Properties?.GetTokens(),
685685
AuthenticationProperties = auth.Properties
686686
};
687687
}
@@ -726,7 +726,7 @@ public virtual async Task<IdentityResult> UpdateExternalAuthenticationTokensAsyn
726726
/// <param name="redirectUrl">The external login URL users should be redirected to during the login flow.</param>
727727
/// <param name="userId">The current user's identifier, which will be used to provide CSRF protection.</param>
728728
/// <returns>A configured <see cref="AuthenticationProperties"/>.</returns>
729-
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string provider, string redirectUrl, string userId = null)
729+
public virtual AuthenticationProperties ConfigureExternalAuthenticationProperties(string? provider, string? redirectUrl, string? userId = null)
730730
{
731731
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
732732
properties.Items[LoginProviderKey] = provider;
@@ -743,7 +743,7 @@ public virtual AuthenticationProperties ConfigureExternalAuthenticationPropertie
743743
/// <param name="userId">The user whose is logging in via 2fa.</param>
744744
/// <param name="loginProvider">The 2fa provider.</param>
745745
/// <returns>A <see cref="ClaimsPrincipal"/> containing the user 2fa information.</returns>
746-
internal static ClaimsPrincipal StoreTwoFactorInfo(string userId, string loginProvider)
746+
internal static ClaimsPrincipal StoreTwoFactorInfo(string userId, string? loginProvider)
747747
{
748748
var identity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme);
749749
identity.AddClaim(new Claim(ClaimTypes.Name, userId));
@@ -781,7 +781,7 @@ await UserManager.GetTwoFactorEnabledAsync(user) &&
781781
/// <param name="loginProvider">The login provider to use. Default is null</param>
782782
/// <param name="bypassTwoFactor">Flag indicating whether to bypass two factor authentication. Default is false</param>
783783
/// <returns>Returns a <see cref="SignInResult"/></returns>
784-
protected virtual async Task<SignInResult> SignInOrTwoFactorAsync(TUser user, bool isPersistent, string loginProvider = null, bool bypassTwoFactor = false)
784+
protected virtual async Task<SignInResult> SignInOrTwoFactorAsync(TUser user, bool isPersistent, string? loginProvider = null, bool bypassTwoFactor = false)
785785
{
786786
if (!bypassTwoFactor && await IsTfaEnabled(user))
787787
{
@@ -809,7 +809,7 @@ protected virtual async Task<SignInResult> SignInOrTwoFactorAsync(TUser user, bo
809809
return SignInResult.Success;
810810
}
811811

812-
private async Task<TwoFactorAuthenticationInfo> RetrieveTwoFactorInfoAsync()
812+
private async Task<TwoFactorAuthenticationInfo?> RetrieveTwoFactorInfoAsync()
813813
{
814814
var result = await Context.AuthenticateAsync(IdentityConstants.TwoFactorUserIdScheme);
815815
if (result?.Principal != null)
@@ -849,7 +849,7 @@ protected virtual Task<SignInResult> LockedOut(TUser user)
849849
/// </summary>
850850
/// <param name="user">The user</param>
851851
/// <returns>Null if the user should be allowed to sign in, otherwise the SignInResult why they should be denied.</returns>
852-
protected virtual async Task<SignInResult> PreSignInCheck(TUser user)
852+
protected virtual async Task<SignInResult?> PreSignInCheck(TUser user)
853853
{
854854
if (!await CanSignInAsync(user))
855855
{
@@ -876,9 +876,9 @@ protected virtual Task ResetLockout(TUser user)
876876
return Task.CompletedTask;
877877
}
878878

879-
internal class TwoFactorAuthenticationInfo
879+
internal sealed class TwoFactorAuthenticationInfo
880880
{
881-
public string UserId { get; set; }
882-
public string LoginProvider { get; set; }
881+
public string? UserId { get; set; }
882+
public string? LoginProvider { get; set; }
883883
}
884884
}

src/Identity/Core/src/TwoFactorSecurityStampValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public TwoFactorSecurityStampValidator(IOptions<SecurityStampValidatorOptions> o
3030
/// </summary>
3131
/// <param name="principal">The principal to verify.</param>
3232
/// <returns>The verified user or null if verification fails.</returns>
33-
protected override Task<TUser> VerifySecurityStamp(ClaimsPrincipal principal)
33+
protected override Task<TUser?> VerifySecurityStamp(ClaimsPrincipal? principal)
3434
=> SignInManager.ValidateTwoFactorSecurityStampAsync(principal);
3535

3636
/// <summary>

src/Identity/Extensions.Core/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetAuthenticationTokenA
895895
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetEmailAsync(TUser! user, string? email) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
896896
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetLockoutEnabledAsync(TUser! user, bool enabled) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
897897
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetLockoutEndDateAsync(TUser! user, System.DateTimeOffset? lockoutEnd) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
898-
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetPhoneNumberAsync(TUser! user, string! phoneNumber) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
898+
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetPhoneNumberAsync(TUser! user, string? phoneNumber) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
899899
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetTwoFactorEnabledAsync(TUser! user, bool enabled) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
900900
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.SetUserNameAsync(TUser! user, string? userName) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!
901901
virtual Microsoft.AspNetCore.Identity.UserManager<TUser>.UpdateAsync(TUser! user) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.IdentityResult!>!

src/Identity/Extensions.Core/src/UserManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ public virtual async Task<IdentityResult> ChangeEmailAsync(TUser user, string ne
15911591
/// The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/>
15921592
/// of the operation.
15931593
/// </returns>
1594-
public virtual async Task<IdentityResult> SetPhoneNumberAsync(TUser user, string phoneNumber)
1594+
public virtual async Task<IdentityResult> SetPhoneNumberAsync(TUser user, string? phoneNumber)
15951595
{
15961596
ThrowIfDisposed();
15971597
var store = GetPhoneNumberStore();

src/Identity/UI/src/Areas/Identity/Pages/V4/Account/ConfirmEmail.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class ConfirmEmailModel : PageModel
2222
/// directly from your code. This API may change or be removed in future releases.
2323
/// </summary>
2424
[TempData]
25-
public string StatusMessage { get; set; }
25+
public string? StatusMessage { get; set; }
2626

2727
/// <summary>
2828
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used

src/Identity/UI/src/Areas/Identity/Pages/V4/Account/ConfirmEmailChange.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class ConfirmEmailChangeModel : PageModel
2222
/// directly from your code. This API may change or be removed in future releases.
2323
/// </summary>
2424
[TempData]
25-
public string StatusMessage { get; set; }
25+
public string? StatusMessage { get; set; }
2626

2727
/// <summary>
2828
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used

0 commit comments

Comments
 (0)