okta - asp.net - How do I get the User Name / Log In Id / Unique Identifier of the current user sign in? - Standard Way?

懵懂的女人 提交于 2019-12-11 04:19:32

问题


Using okta, how do I get the User Name / Log In Id / Unique Identifier of the current user signed in?

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;

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.

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.


回答1:


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");
    ...
}



回答2:


This is the code I added:

var userId = principal.Claims.FirstOrDefault(c => c.Type == "preferred_username");


来源:https://stackoverflow.com/questions/57443361/okta-asp-net-how-do-i-get-the-user-name-log-in-id-unique-identifier-of-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!