add nullableUserId to IUserContext.cs
This commit is contained in:
parent
db012aed92
commit
77cfacb95e
2 changed files with 16 additions and 4 deletions
|
|
@ -24,8 +24,19 @@ public class UserContext : IUserContext
|
||||||
/// Возвращает UserId из токена, при отсуствии кидает Unauthorized
|
/// Возвращает UserId из токена, при отсуствии кидает Unauthorized
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="UnauthorizedAccessException"></exception>
|
/// <exception cref="UnauthorizedAccessException"></exception>
|
||||||
public int UserId =>
|
//public int UserId =>
|
||||||
int.Parse(
|
// int.Parse(
|
||||||
_httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)
|
// _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? throw new UnauthorizedException("Where's your token mister"));
|
||||||
?.Value ?? throw new UnauthorizedException("Where's your token mister"));
|
private string? ClaimValue
|
||||||
|
=> _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
|
||||||
|
public int UserId
|
||||||
|
=> int.TryParse(ClaimValue, out var id)
|
||||||
|
? id
|
||||||
|
: throw new UnauthorizedException("User ID claim missing or malformed");
|
||||||
|
|
||||||
|
public int? NullableUserId
|
||||||
|
=> int.TryParse(ClaimValue, out var id)
|
||||||
|
? id
|
||||||
|
: null;
|
||||||
}
|
}
|
||||||
|
|
@ -3,4 +3,5 @@ namespace SurveyBackend.Core.Contexts;
|
||||||
public interface IUserContext
|
public interface IUserContext
|
||||||
{
|
{
|
||||||
int UserId { get; }
|
int UserId { get; }
|
||||||
|
int? NullableUserId { get; }
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue