added UserContext for easy UserId getting
started writing custom services to implement users logic
This commit is contained in:
parent
d810101033
commit
7a1078457d
6 changed files with 83 additions and 6 deletions
19
SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs
Normal file
19
SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using System.Security.Claims;
|
||||
using SurveyBackend.Core.Contexts;
|
||||
|
||||
namespace SurveyBackend.Contexts;
|
||||
|
||||
public class UserContext : IUserContext
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public UserContext(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public int UserId =>
|
||||
int.Parse(
|
||||
_httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)
|
||||
?.Value ?? throw new UnauthorizedAccessException());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue