Antediluvian
Antediluvian

Reputation: 733

.net core web api jwt

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?

Upvotes: 0

Views: 75

Answers (1)

Robert Perry
Robert Perry

Reputation: 1954

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);
    }

Upvotes: 1

Related Questions