我怎样才能在不使用授权属性User.Identity值?

问题描述 投票:0回答:1

我正在开发这是使用JwtBearer为authentication.When我尝试使用User.Identity这样的API;

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

用户不包含令牌/用户数据和User.Identity.IsAuthenticated总是返回false。但是,如果我添加

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

这让用户的数据,但我想未认证的用户可以访问了。

我怎么能做到这一点没有授权过滤器?

.net authentication asp.net-core .net-core asp.net-apicontroller
1个回答
1
投票

添加[AllowAnonymous]属性,还有,如果你想允许未经授权的用户。您必须[Authorize]属性要建立用户主体。

© www.soinside.com 2019 - 2024. All rights reserved.