使用Identity Server登录的客户端,访问被Identity Server保护的API时,通过bearer和access_token访问API,在API端可以使用User.Claim获取Claims信息:
namespace MyWebApi.Controllers
{
[Route("identity")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var claims = User.Claims.ToList();
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
}
但是会发现,不是所有的Claim都包括在access_token传递过来,只包括客户端id等少数系信息。如果希望包括更多的User Claim信息,需要在Api Resources中显示定义。

网友评论