经过一番努力(以及大量的教程、指南等) ,我设法建立了一个小。NET 核心 REST Web API,当存储的用户名和密码有效时,使用 Auth 控制器发出 JWT 令牌。
令牌将用户 ID 存储为子声明。
我还设置了 WebAPI,以便在方法使用 Authorize 注释时验证这些令牌。
app.UseJwtBearerAuthentication(...)
Now my question: How do I read the user id (stored in the subject claim) in my controllers (in a Web API)?
It is basically this question (How do I get current user in ASP .NET Core) but I need an answer for a web api. And I do not have a UserManager. So I need to read the subject claim from somewhere.