0

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();
            }
        }
    }
Abhi
  • 126
  • 1
  • 9
  • More likely than not, you're relying on cookie-based authentication, which doesn't work with Web Api. You need to authorize by sending a bearer token in the `Authorization` header of the request. – Chris Pratt Aug 15 '17 at 19:03
  • @Abhi How does user login? Could you show your authentication mechanism? – Win Aug 15 '17 at 22:50
  • @win i am using normal Individual user account authentication. – Abhi Aug 16 '17 at 03:28
  • @Chris Patt Can you give more information on sending a bearer token in the Authorization header of the request. – Abhi Aug 16 '17 at 03:30
  • Perhaps this may help you: https://stackoverflow.com/questions/38661090/token-based-authentication-in-web-api-without-any-user-interface/38670221#38670221 –  Aug 21 '17 at 17:51

0 Answers0