简体   繁体   English

在Asp.Net Core Controller中获取用户名

[英]Get username in Asp.Net Core Controller

What is the accepted way to get the username of a Asp.Net Core REST service inside of the controller? 在控制器内部获取Asp.Net Core REST服务的用户名的可接受方法是什么? Assuming the authentication is done by JWT? 假设身份验证是通过JWT完成的?

I see User.Claims.First().Value contains the username, but is it always going to be in the first claim? 我看到User.Claims.First().Value包含用户名,但是它总是会出现在第一个声明中吗?

Or is there some better way to access it? 还是有更好的方法来访问它?

You can use the FindFirst() function on the ClaimsPrincipal class. 您可以在ClaimsPrincipal类上使用FindFirst()函数。

Examples: 例子:

// using built in claim types
User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier); 

// using custom claim type
User.FindFirst("username");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM