Skip to content

Commit 6bd9dbb

Browse files
Copilothalter73
andcommitted
Update RefreshSignInAsync documentation and fix null checks
- Updated XML documentation to clearly state user must be signed in - Added explicit null check for auth result at the beginning - Removed unnecessary null-conditional operators after auth.Succeeded check - All existing tests pass Co-authored-by: halter73 <[email protected]>
1 parent 45900c9 commit 6bd9dbb

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Identity/Core/src/SignInManager.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,14 @@ public virtual async Task<bool> CanSignInAsync(TUser user)
165165
}
166166

167167
/// <summary>
168-
/// Signs in the specified <paramref name="user"/>, whilst preserving the existing
168+
/// Refreshes the sign-in for the specified <paramref name="user"/>, whilst preserving the existing
169169
/// AuthenticationProperties of the current signed-in user like rememberMe, as an asynchronous operation.
170170
/// </summary>
171-
/// <param name="user">The user to sign-in.</param>
171+
/// <remarks>
172+
/// The user must already be signed in, and the user ID must match the currently authenticated user.
173+
/// If the user is not signed in, use <see cref="SignInAsync(TUser, bool, string)"/> instead.
174+
/// </remarks>
175+
/// <param name="user">The user to refresh the sign-in for.</param>
172176
/// <returns>The task object representing the asynchronous operation.</returns>
173177
public virtual async Task RefreshSignInAsync(TUser user)
174178
{
@@ -189,6 +193,12 @@ public virtual async Task RefreshSignInAsync(TUser user)
189193
private async Task<(bool success, bool? isPersistent)> RefreshSignInCoreAsync(TUser user)
190194
{
191195
var auth = await Context.AuthenticateAsync(AuthenticationScheme);
196+
if (auth == null)
197+
{
198+
Logger.LogError("RefreshSignInAsync prevented because authentication result is null.");
199+
return (false, null);
200+
}
201+
192202
if (!auth.Succeeded || auth.Principal?.Identity?.IsAuthenticated != true)
193203
{
194204
Logger.LogError("RefreshSignInAsync prevented because the user is not currently authenticated. Use SignInAsync instead for initial sign in.");
@@ -204,8 +214,8 @@ public virtual async Task RefreshSignInAsync(TUser user)
204214
}
205215

206216
IList<Claim> claims = Array.Empty<Claim>();
207-
var authenticationMethod = auth.Principal?.FindFirst(ClaimTypes.AuthenticationMethod);
208-
var amr = auth.Principal?.FindFirst("amr");
217+
var authenticationMethod = auth.Principal.FindFirst(ClaimTypes.AuthenticationMethod);
218+
var amr = auth.Principal.FindFirst("amr");
209219

210220
if (authenticationMethod != null || amr != null)
211221
{
@@ -221,7 +231,7 @@ public virtual async Task RefreshSignInAsync(TUser user)
221231
}
222232

223233
await SignInWithClaimsAsync(user, auth.Properties, claims);
224-
return (true, auth.Properties?.IsPersistent ?? false);
234+
return (true, auth.Properties.IsPersistent);
225235
}
226236

227237
/// <summary>

0 commit comments

Comments
 (0)