Add a property to retrieve the claims (id and login) in the token

This commit is contained in:
Sébastien André
2020-03-20 19:34:27 +01:00
parent 7bad7e80d5
commit c15ad54823
11 changed files with 44 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AircraftController : ControllerBase
public class AircraftController : Base
{
public AircraftController(IAircraftService aircraftService,
IMapper mapper)

View File

@@ -0,0 +1,30 @@
using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers
{
public class Base : ControllerBase
{
public User ConnectedUser
{
get
{
if (_connectedUser == null)
{
_connectedUser = new User();
_connectedUser.Login = User.Claims.Single(c => c.Type == ClaimTypes.Name).Value;
_connectedUser.Id = Convert.ToInt32(User.Claims.Single(c => c.Type == ClaimTypes.UserData).Value);
}
return _connectedUser;
}
}
private User _connectedUser;
}
}

View File

@@ -13,7 +13,7 @@ namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DropZoneController : ControllerBase
public class DropZoneController : Base
{
public DropZoneController(IDropZoneService dropZoneService,
IMapper mapper)

View File

@@ -13,7 +13,7 @@ namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class GearController : ControllerBase
public class GearController : Base
{
public GearController(IGearService gearService,
IMapper mapper)

View File

@@ -10,13 +10,15 @@ using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract;
using skydiveLogs_api.Model;
using System.Linq;
using System.Security.Claims;
namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class JumpController : ControllerBase
public class JumpController : Base
{
public JumpController(IJumpService jumpService,
IMapper mapper)
@@ -30,8 +32,7 @@ namespace skydiveLogs_api.Controllers
[EnableCors]
public IEnumerable<JumpResp> Get()
{
var connectedUser = new User() { Id = 1 }; // TEST
var result = _jumpService.GetAllJumps(connectedUser);
var result = _jumpService.GetAllJumps(ConnectedUser);
return _mapper.Map<IEnumerable<JumpResp>>(result);
}
@@ -55,7 +56,7 @@ namespace skydiveLogs_api.Controllers
value.JumpTypeId,
value.GearId,
_mapper.Map<Jump>(value),
null /* Provenant du token */);
ConnectedUser);
}
// PUT: api/Jump/5

View File

@@ -13,7 +13,7 @@ namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class JumpTypeController : ControllerBase
public class JumpTypeController : Base
{
public JumpTypeController(IJumpTypeService jumpTypeService,
IMapper mapper)

View File

@@ -12,7 +12,7 @@ namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StatsController : ControllerBase
public class StatsController : Base
{
public StatsController(IStatsService statsService,
IMapper mapper)

View File

@@ -67,6 +67,7 @@ namespace skydiveLogs_api
var iocService = new IocService(services, Configuration);
iocService.Configure();
// AutoMapper
services.AddAutoMapper(typeof(Mapper.ModelProfile));
}

View File

@@ -11,7 +11,7 @@
},
"JWT": {
"Issuer": "NoIdea",
"Key": "the very long and strong passphrase to crypt the token for DEV"
"Key": "the very long and strong passphrase to encrypt the token for DEV"
},
"AllowedHosts": "*",
"ConnectionStrings": {

View File

@@ -9,7 +9,7 @@
},
"JWT": {
"Issuer": "NoIdea",
"Key": "the very long and strong passphrase to crypt the token for RELEASE"
"Key": "the very long and strong passphrase to encrypt the token for RELEASE"
},
"AllowedHosts": "*",
"ConnectionStrings": {

View File

@@ -11,7 +11,7 @@
},
"JWT": {
"Issuer": "NoIdea",
"Key": "the very long and strong passphrase to crypt the token"
"Key": "the very long and strong passphrase to encrypt the token"
},
"AllowedHosts": "*",
"ConnectionStrings": {