asp.net-core - Get User Info From Access Token WebApi Asp.net Core
问题描述
I'm using the new ASP.NET Core Identity API Authorization found in dotnet-core3-preview, the docs are found here https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0
I'm succesfully running the typical login process, and the token are set and sent in the Bearer token. However rightnow I have an api end point that should return some user details from the database, so I'm trying to extract the user id from the token to query the database.
Yet, I'm not able to find the id in any of the claims, as per my screenshot below, how can I accomplish this?
[HttpGet]
[Authorize]
public async Task<IActionResult> GetUserByToken(){
var ls = User.Claims.AsQueryable();
return Ok(ls);
}
解决方案
用户 ID 可以在声明中找到: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier
:
var userID = User.Claims.Where(a => a.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value;
该 id 值等于由 ASP.NET Identity 创建的表中的Id
列。AspNetUsers
推荐阅读
- java - 如何解析 Json 数据并将它们传递给另一个活动?
- python - python - 试图做一个程序来替换给定的行,用同一行但全部大写
- php - MySQL Join 查询信用余额
- javascript - 从循环中传递 javascript 函数中的变量
- c++ - 如何使用 LibGit2 C++ 列出分支?
- angular - 在 Angular 中使用 HTTP PUT 方法添加数组
- reactjs - React Native - setState 在构造函数中不起作用
- r - 如何在 R-studio 中更改或重置绘图(ACF)设备中的参数
- c++ - 人口普查转换与互信息
- react-native - 在 expo.sendPushNotificationsAsync 中更改声音