Problem accessing ClaimsPrincipal.Identity.Name in http endpoint

I’m trying to retrieve the identity of the user who is accessing my endpoint. The endpoint is permissioned with a token and is working fine. However, in the endpoint code when I try to access ClaimsPrincipal.Identity.Name it is empty. If I change the endpoint to instead return ClaimsPrincipal object, I’m able to access all of the objects inside ClaimsPrincipal including Identity.Name. Why am I not able to do so from within the endpoint code?

You should be able to use the $User variable to get the username without using the $ClaimsPrinciple object.

That said, I have no idea why it would be different between returning the value and accessing it internally.

The $User variable works in Apps but not http endpoints. I tried to use $User in the endpoint code but it is empty.

I’m using version 5.0.15 could this be a bug?

I just tried this on 5.3 and I guess I didn’t realize the variables were different. We need to get this fixed. Can you try $UserName? I also had a null $user but $username worked.

Good news $UserName is working.
So to recap $User works for web apps and $UserName is working for endpoints.
Thank you!

Actually it looks like $UserName works for both endpoints and web apps while $User seems to only work for web apps but null for endpoints. And as stated before the ClaimsPrincipal object is not usable within the code in the endpoint. Not an issue for me at the moment since all I am currently looking for is the user’s name but this could be a problem for other cases where it may be neccessary to retrieve roles or other items associated with the user.

1 Like