okta-asp.net-如何获取当前用户登录的用户名/登录ID/唯一标识符? -标准方式? [英] okta - asp.net - How do I get the User Name / Log In Id / Unique Identifier of the current user sign in? - Standard Way?
问题描述
使用okta,如何获取当前登录用户的用户名/登录ID/唯一标识符?
Using okta, how do I get the User Name / Log In Id / Unique Identifier of the current user signed in?
我目前正在使用ASP.Net Web窗体应用程序.用户成功登录后,我可以使用以下代码获取用户名.
I am currently working on an ASP.Net Web Forms App. After the user successfully signs in, I can get the name of the user with the code below.
var name = User.Identity.Name;
但是,我想要用户的登录ID/唯一标识符.查看okta网站上的个人资料"表单,看起来okta会调用该字段用户名".
However, I want the Log In Id / Unique Identifier of the User. Looking at the Profile form on the okta web site, it looks like okta calls that field User Name.
进行了一些研究之后,我确实编写了一些代码,这些代码使用ClaimsPrincipal类和ClaimType =="preferred_username"获得了我想要的.但是代码对我来说似乎是一个hack.寻找获得此值的标准方法.
After doing some research, I did write up some code that gets what I want using the ClaimsPrincipal class and claimType == "preferred_username". But the code seems like a hack to me. Looking for the Standard Way to get this value.
推荐答案
是的,您在正确的轨道上.您必须根据其类型"来定位声明,但这很简单:
Yes, you're on the right track. You do have to locate a claim based on its "type" but it's simple enough:
[Authorize]
public ActionResult SomeMethod()
{
var userId = HttpContext.User.FindFirstValue("preferred_username");
...
}
这篇关于okta-asp.net-如何获取当前用户登录的用户名/登录ID/唯一标识符? -标准方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!