Getting the logged-in username in ASP.NET

I am trying to add Okta SSO to my ASP.NET 4.7 application. I’ve managed to login using the redirect to Okta’s login portal, but beyond verifying that someone “is authorized” I haven’t been able to do anything with the login.

Specifically I need to get at minimum the username of the logged in user, to use in displaying their profile and app settings, after okta redirects back to my app.

In the codebehind (C#) I’ve tried looking through the Request.Headers and the Request.QueryString, but I haven’t found anything useful there.

How can I get the username of the currently logged in user?

Hi @shutchinson

You can use the subject claim “sub” from the access token in order to retrieve the username of the user that signed in to your application via Okta.

Thanks @dragos for your response.

Could you elaborate, maybe with either a code sample or a link to the documentation where this is explained? I also don’t understand how to get the access token, since right now that is all hidden away while it happens.

I followed the example given on this page, so I have my Startup.cs file and then a call to

HttpContext.Current.GetOwinContext().Authentication.Challenge( new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);

I also need this exact question explained, preferably with a code sample so I can visualize. Many thanks!