I am using a post method where comedyId is coming from body of the request, while using postman i am getting redirected to login page again and again even after login.
Why login is not working for Api and User.Identity.GetUserId() is also returning null value ?
namespace ComedyCentral.Controllers.Api
{
[Authorize]
public class AttendancesController : ApiController
{
private ApplicationDbContext _context;
public AttendancesController()
{
_context = new ApplicationDbContext();
}
[HttpPost]
public IHttpActionResult Attend([FromBody] int comedyId)
{
var userId = User.Identity.GetUserId();
if (_context.Attendances.Any(a => a.AttendeeId == userId && a.ComedyId == comedyId))
return BadRequest("The attendance already exists.");
var attendance = new Attendance
{
ComedyId = comedyId,
AttendeeId = userId
};
_context.Attendances.Add(attendance);
_context.SaveChanges();
return Ok();
}
}
}