Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user name from Azure authenticated AD in .Net Web API

Tags:

c#

.net

azure

I have a web API that is authenticated by Azure's AD. Within the API codes, what is the code to retrieve the username of the authenticated user?

like image 765
Kelvin Yeo Avatar asked Oct 19 '25 10:10

Kelvin Yeo


1 Answers

Using System.Web.HttpContext.Current.User.Identity.Name will retrieve the email address that they have used to log in with.

As a side note, I am using this to control what the user has access to on the site. Because of how I am implementing it, it is more convenient for me to maintain a database of the usernames and their roles. However, if you are doing the same thing, it is also worth looking into [Authorize] and maintaining site access that way.

like image 172
JCollerton Avatar answered Oct 21 '25 01:10

JCollerton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!