1+ using System . Security . Claims ;
12using Microsoft . AspNetCore . Mvc ;
23
34namespace Initium . Controllers ;
@@ -10,9 +11,30 @@ namespace Initium.Controllers;
1011/// </summary>
1112public abstract class BaseController : ControllerBase
1213{
13- // protected string? GetClaim(string claimType) => HttpContext.User.Claims.FirstOrDefault(c => c.Type == claimType)?.Value;
14- // protected string UserId => HttpContext.User.Claims.First(claim => claim.Type == "sub").Value;
15- // protected Guid UserId => Guid.TryParse(User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier)?.Value, out var authorizationClaimUid) ? authorizationClaimUid : default;
14+ /// <summary>
15+ /// Gets the GUID of the currently authenticated user, if available.
16+ /// Extracted from the "sub" or "NameIdentifier" claims.
17+ /// </summary>
18+ protected Guid ? UserId => Guid . TryParse ( User . FindFirst ( "sub" ) ? . Value ?? User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value , out var guid ) ? guid : null ;
19+
20+ /// <summary>
21+ /// Gets the IP address of the client making the current request.
22+ /// </summary>
23+ protected string ? ClientIp => HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
24+
25+ /// <summary>
26+ /// Gets the User-Agent string from the current HTTP request headers.
27+ /// </summary>
28+ protected string ? UserAgent => HttpContext . Request . Headers . UserAgent ;
29+
30+ /// <summary>
31+ /// Retrieves the specified query string parameter value from the current HTTP request.
32+ /// Returns null if the parameter does not exist.
33+ /// </summary>
34+ /// <param name="key">The query parameter key.</param>
35+ /// <returns>The parameter value if found; otherwise, null.</returns>
36+ protected string ? GetQueryParameter ( string key ) =>
37+ HttpContext . Request . Query . TryGetValue ( key , out var value ) ? value . FirstOrDefault ( ) : null ;
1638}
1739
1840// public class BaseController<TService>(TService service) : ApiController where TService : BaseService
0 commit comments