【发布时间】:2023-03-20 18:28:02
【问题描述】:
我在我的项目中使用 Web Api 2 Auth 模板。我想在客户端使用 Api Key 请求时获取用户 ID。我的问题是:
1 如何获取 api 客户端用户 ID。
-是对还是错通过Api请求解决?
-还是直接取http头中的userId?
-或者使用Api键从数据库中查询?
2 我创建了继承自 ApiController 的 MyApiController,我想在其中获取声明。所以我在其中编写了一些代码。像这样
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
var principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
CorpId = (from c in principal.Claims where c.Type.Contains("CorpId") select c.Value).FirstOrDefault();
UserId = (from c in principal.Claims where c.Type.Contains("UserId") select c.Value).FirstOrDefault();
}
我发现 MyApiController 中的声明为空。但是当我在动作控制器中这样做时,它有效吗?
【问题讨论】:
-
你解决了吗?我也有同样的问题
标签: api web oauth asp.net-web-api2