问题
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