Get claims from a WebAPI Controller - JWT Token,

后端 未结 4 1938
一向
一向 2020-12-05 18:53

I have built an application which uses JWT bearer authentication in ASP.NET Core. When authenticating I define some custom claims which i need to read in another WebAPI con

4条回答
  •  佛祖请我去吃肉
    2020-12-05 19:24

    In any controller from net core 2 that has gone through the authorize with the JwtBearerDefaults scheme, you can use:

     [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
     public ActionResult Index()
        {
            var user = User.FindFirst("Name").Value;
            //or if u want the list of claims
            var claims = User.Claims;
    
            return View();
        }
    

提交回复
热议问题