当前用户在.NET 5中
我正在使用 .NET 5 和 Identity 4。 当我想获取当前的 userId 或声明时,出现非常奇怪的错误。我无法在 Api 控制器中获取当前用户,但 Odata 控制器通常返回用户的数据和声明。
因此,我在两个控制器中键入以下代码:
ClaimsPrincipal user = User;
if (user != null)
{
string userId = user.Claims.Where(c => c.Type == "sub").FirstOrDefault()?.Value;
}
在该示例中,我需要在 api 控制器中获取 userID。我在 odata 控制器中获取了 userID,没有任何问题。
我用谷歌搜索了很多时间,现在我没有想象,我向你寻求帮助。 有人遇到过类似的问题吗?
I am using .NET 5 and identity 4.
I have very strange error when I want to get current userId, or claims. I can't get current user in Api controller, but Odata controller return normally user's data and claims..
So, this code I type in both controllers:
ClaimsPrincipal user = User;
if (user != null)
{
string userId = user.Claims.Where(c => c.Type == "sub").FirstOrDefault()?.Value;
}
In that example I need to get userID in api controller. I get userID in odata controller without any problem.
I google lot of time and now I don't have imagine and I ask you for help.
Has anyone had a similar problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我忘记在API控制器上设置[授权]
I forget to set [Authorize] on Api controller