3

I'm developing an api which is using JwtBearer for authentication.When I'm try to using User.Identity like this;

    [HttpGet]
    public IActionResult Detail(int id)
    {
       if(User.Identity.IsAuthenticated)
       {
          return ...
       }
       else
       {
          return ..
       }
    }

User not contain token/user data and User.Identity.IsAuthenticated always return false. But if i add

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

This get User data but i want unauthenticated user can access too.

How could I do this without an Authorize filter?

p3ngu3n
  • 55
  • 9

1 Answers1

4

Add the [AllowAnonymous] attribute, as well, if you want to allow unauthenticated users. You must have [Authorize] attribute for the user principal to be established.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I add `[Authorize]`on controller , `[AllowAnonymous]` on action and its solve my problem. Thanks for help. – p3ngu3n Feb 04 '19 at 20:26