0

I have a web api project where:

  1. some controller actions need a JWT authentication,
  2. some do not need a JWT authentication,
  3. some return different data depending on the validity of the JWT provided where the data only differs in one field's value.

The 1st one is easy as the JWT authentication middleware will handle it but how can I achieve the other 2?

1
  • are you using MVC?
    – Konrad
    Jun 10, 2019 at 23:17

1 Answer 1

1

2). Add the [AllowAnonymous] attribute to the endpoint or the controller to turn off authentication for that endpoint/controller.

3). You will need to do a check on the Users Claims (provided you have added the different permissions into there - and the apply logic to your return values depending on those values. If you return an IHttpActionResult like so, you can dynamically change the return values depending on your logic.

    public IHttpActionResult GetProduct(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.