Skip to content

Commit c5a6588

Browse files
author
Vadim Belov
committed
Add support for custom token claims in BaseAuthController
Introduce virtual GetAdditionalTokenClaims method to allow derived controllers to add custom claims to authentication tokens. Update CreateAccessToken to include these claims in JWT generation. Add XML documentation for the new method.
1 parent cb8d0a7 commit c5a6588

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Sources/EasyExtensions.AspNetCore.Authorization/Controllers/BaseAuthController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@ public virtual TimeSpan GetCookieExpirationTime()
379379
return TimeSpan.FromDays(30);
380380
}
381381

382+
/// <summary>
383+
/// Retrieves additional token claims for the specified user to be included in authentication tokens.
384+
/// </summary>
385+
/// <param name="userId">The unique identifier of the user for whom to retrieve additional token claims.</param>
386+
/// <returns>An enumerable collection of key-value pairs representing additional claims to be added to the user's
387+
/// authentication token. The collection is empty if no additional claims are available.</returns>
388+
public virtual IEnumerable<KeyValuePair<string, string>> GetAdditionalTokenClaims(Guid userId)
389+
{
390+
return [];
391+
}
392+
382393
private string CreateAccessToken(Guid userId, IEnumerable<string> roles)
383394
{
384395
return _tokenProvider.CreateToken(cb =>
@@ -388,6 +399,10 @@ private string CreateAccessToken(Guid userId, IEnumerable<string> roles)
388399
{
389400
cb.Add(ClaimTypes.Role, role);
390401
}
402+
foreach (var claim in GetAdditionalTokenClaims(userId))
403+
{
404+
cb.Add(claim.Key, claim.Value);
405+
}
391406
return cb;
392407
});
393408
}

0 commit comments

Comments
 (0)