在构造Asp.Net等基于.Net Framework等应用时,Web Api或者其它后台往往与前台处于同一进程,一般通过Session或者其它方式获取用户信息。采用bearer JWT认证的Web Api与被调用的前端处于不同的进程甚至不同的服务器,因此不能使用Session等获取用户信息。用户信息通过JWT传递到API,被平台填充到User.Claims中,可以直接获取,代码如下:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Api.Controllers
{
[Route("identity")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
}
网友评论